c++ - 如何查找给定键是否存在于 C++ std::map 中

标签 c++ dictionary stl

我正在尝试检查给定的键是否在 map 中并且有些无法做到:

typedef map<string,string>::iterator mi;
map<string, string> m;
m.insert(make_pair("f","++--"));
pair<mi,mi> p = m.equal_range("f");//I'm not sure if equal_range does what I want
cout << p.first;//I'm getting error here

那么如何打印 p 中的内容?

最佳答案

使用 map::findmap::end :

if (m.find("f") == m.end()) {
  // not found
} else {
  // found
}

关于c++ - 如何查找给定键是否存在于 C++ std::map 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1939953/

相关文章:

c++ - hash_map 会自动排序 [C++] 吗?

c++ - 高效计算随机误差

python - 随机 "int is not subscriptable"行为

c++ - 从 ifstream 文件读取给出错误的大小

c++ - 如何在 C++ 中获取不同 vector 的 vector

c++ - C++ 中的 Controller /迷你内核设计模式

c++ - 如何在不将代码放入头文件的情况下处理任何迭代器?

python-3.x - 将字典键和值添加到 Redis 列表

C#继承自Dictionary,遍历KeyValuePairs

c++ - 为什么迭代器定义为结构而不是类?