gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:48:50 25 4
gpt4 key购买 nike

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

所以我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com