c++ - 如何在 int、string 的 map 上使用查找

标签 c++ dictionary

这可能很简单,但我醒得太久了。

所以我有一个 map 并且我正在尝试使用 find 函数

iter=mapInKey.find(int)->second;
if (iter != mapInKey.end() )
{
    outFileTxt << iter;
}

但我得到这个错误:no matching function for call to std::map<int, std::basic_string<char> >::find(std::string&)

我只想输出另一对int值

最佳答案

像这样

    std::map <int,std::string>::iterator it = mapInKey.find( val ); //val is the integer key you search for

    if(it != mapInKey.end()){
     // do something 
     cout<< it->second; // this will print the string
   }

编辑

我想你想按字符串搜索。那么你宣布 map 错误。用这个

map<string,int> m;
m["Hi"]= 1;
m["Hello"]= 2;
map<string,int>::iterator it = m.find("Hello");
if(it != m.end())
{
  cout<< it->first<<":"<<it->second<<endl;
}

关于c++ - 如何在 int、string 的 map 上使用查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23507859/

相关文章:

python - 在python字典中排序时间戳

python - 拉取包含字符串的列的部分并创建字典,可以根据公共(public)键拉取值

android - map 不显示路况信息

java - map 中的关键存在检查实用程序

c++ - Makefile 不使用包含/库路径?

c++ - 替换文档的根节点

c++ - 寻找下一个素数算法

c++ - 在 Visual Studio C++ 资源编辑器中导入 png 文件

C++ 在将文本文件读入字符串时如何忽略文本文件中的符号/数字?

c++ - 文件的 O_DIRECT 标志