使用find()、count()或C++20的contains()可判断std::map中键是否存在;推荐find()因能同时获取值且避免重复查找,C++20中contains()语义更清晰;需避免operator[]隐式插入导致的意外行为。

在C++的
std::map
count()
find()
contains()
要检查
std::map
find()
contains()
1. 使用 map::find()
find()
map::end()
find()
map::end()
立即学习“C++免费学习笔记(深入)”;
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> scores = {
{"Alice", 95},
{"Bob", 88},
{"Charlie", 72}
};
std::string keyToFind1 = "Alice";
std::string keyToFind2 = "David";
// 检查 "Alice"
auto it1 = scores.find(keyToFind1);
if (it1 != scores.end()) {
std::cout << keyToFind1 << " 存在,分数为: " << it1->second << std::endl;
} else {
std::cout << keyToFind1 << " 不存在。" << std::endl;
}
// 检查 "David"
auto it2 = scores.find(keyToFind2);
if (it2 != scores.end()) {
std::cout << keyToFind2 << " 存在,分数为: " << it2->second << std::endl;
} else {
std::cout << keyToFind2 << " 不存在。" << std::endl;
}
return 0;
}2. 使用 map::count()
count()
map
std::map
count()
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> scores = {
{"Alice", 95},
{"Bob", 88}
};
std::string keyToFind1 = "Bob";
std::string keyToFind2 = "Eve";
if (scores.count(keyToFind1) > 0) { // 或者直接 scores.count(keyToFind1) == 1
std::cout << keyToFind1 << " 存在。" << std::endl;
} else {
std::cout << keyToFind1 << " 不存在。" << std::endl;
}
if (scores.count(keyToFind2)) { // 非0即为真
std::cout << keyToFind2 << " 存在。" << std::endl;
} else {
std::cout << keyToFind2 << " 不存在。" << std::endl;
}
return 0;
}3. 使用 map::contains()
这是C++20引入的一个非常简洁且语义清晰的方法,直接返回一个
bool
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> scores = {
{"Alice", 95},
{"Bob", 88}
};
std::string keyToFind1 = "Alice";
std::string keyToFind2 = "Frank";
if (scores.contains(keyToFind1)) {
std::cout << keyToFind1 << " 存在。" << std::endl;
} else {
std::cout << keyToFind1 << " 不存在。" << std::endl;
}
if (scores.contains(keyToFind2)) {
std::cout << keyToFind2 << " 存在。" << std::endl;
} else {
std::cout << keyToFind2 << " 不存在。" << std::endl;
}
return 0;
}map::find()
在我看来,
map::find()
find()
想象一下,如果你先用
count()
operator[]
at()
count
[]
at
map
find()
it->first
it->second
// 效率对比示例
std::map<std::string, int> data = {{"key1", 100}};
std::string targetKey = "key1";
// 方式一:使用 find()
auto it = data.find(targetKey);
if (it != data.end()) {
// 键存在,直接通过迭代器访问值
std::cout << "find(): " << targetKey << " 存在,值为 " << it->second << std::endl;
}
// 方式二:使用 count() + operator[] (不推荐,可能两次查找)
if (data.count(targetKey)) {
// 键存在,但这里可能进行第二次查找来获取值
std::cout << "count() + []: " << targetKey << " 存在,值为 " << data[targetKey] << std::endl;
}上面
data[targetKey]
data.at(targetKey)
find()
map::count()
map::contains()
这三者在功能上都是为了判断键是否存在,但在设计理念和使用体验上,还是有些细微的差异。
map::count()
size_type
std::map
count()
std::multimap
multimap
count()
std::map
std::map
find()
count()
find()
!= map.end()
map::contains()
bool
true
false
count()
find()
contains()
总结一下,如果能用C++20,用
contains()
find()
count()
find()
在实际的C++开发中,处理
map
find()
count()
潜在的错误:
map[key]
myMap[someKey]
someKey
map
someKey
map
map
std::map<std::string, int> ages; // 假设我们只想查询,但 "Alice" 不存在 int aliceAge = ages["Alice"]; // 错误!"Alice"被插入,值为0 (int的默认值) std::cout << "Map size: " << ages.size() << std::endl; // 输出 1
map.at(key)
at()
at()
std::out_of_range
std::map<std::string, int> ages = {{"Bob", 30}};
try {
int charlieAge = ages.at("Charlie"); // "Charlie" 不存在,抛出异常
} catch (const std::out_of_range& e) {
std::cerr << "错误: " << e.what() << std::endl; // 输出 "map::at"
}最佳实践:
核心原则是:先检查,后访问。
使用 find()
contains()
std::map<std::string, double> grades = {{"Math", 92.5}, {"Physics", 88.0}};
std::string subject = "Chemistry";
// 使用 C++20 contains()
if (grades.contains(subject)) {
std::cout << subject << " 成绩: " << grades.at(subject) << std::endl;
} else {
std::cout << subject << " 成绩未找到。将添加。" << std::endl;
grades[subject] = 90.0; // 此时才安全地插入
}
// 或者使用 find()
auto it = grades.find("Math");
if (it != grades.end()) {
std::cout << "Math 成绩: " << it->second << std::endl;
} else {
// ... 处理未找到的情况
}利用结构化绑定 (C++17) 进行插入判断: 如果你打算插入一个键值对,并且想知道这个键是否已经存在(以及是否成功插入),
insert()
emplace()
std::pair<iterator, bool>
std::map<std::string, int> inventory;
// 尝试插入 "apple"
auto [apple_it, apple_inserted] = inventory.insert({"apple", 10});
if (apple_inserted) {
std::cout << "成功插入 apple, 数量: " << apple_it->second << std::endl;
} else {
std::cout << "apple 已存在, 数量: " << apple_it->second << std::endl;
}
// 再次尝试插入 "apple"
auto [apple_it2, apple_inserted2] = inventory.insert({"apple", 15}); // 不会插入,因为键已存在
if (apple_inserted2) {
std::cout << "成功插入 apple (第二次), 数量: " << apple_it2->second << std::endl;
} else {
std::cout << "apple 再次尝试插入失败, 现有数量: " << apple_it2->second << std::endl;
// 如果想更新,需要单独处理
apple_it2->second = 15;
std::cout << "更新 apple 数量为: " << apple_it2->second << std::endl;
}这种方式在需要“插入或更新”逻辑时特别有用。
何时使用 at()
at()
总的来说,避免
map[key]
find()
contains()
insert()
map
以上就是如何在C++中检查map中是否存在某个键_C++ map键存在性判断方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号