这可能很简单,但我醒得太久了。
所以我有一个 map
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/