如何在int,string类型的映射上使用find

how to use find on a map of int,string

本文关键字:映射 find 类型 int string      更新时间:2023-10-16

这可能很容易,但我已经醒了太久了。

我有一个map<Int,>和我试图使用函数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<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;
}