map.find()和map.end()迭代器据说是不兼容的?

tra*_*mot 0 c++ iterator unordered-map

我在if语句中使用map.find(key)和map.end()函数:

if( p_Repos->returnTypeMap().find(tc[0]) != p_Repos->returnTypeMap().end() ) 
Run Code Online (Sandbox Code Playgroud)

但它不起作用,我得到一个Microsoft Visual C++运行时库错误,告诉我"表达式:列表迭代器不兼容".tc [0]只是一个字符串,我的地图中的键位置是一个字符串.

但是,它们应该兼容,对吗?

任何帮助是极大的赞赏.

谢谢,汤姆

编辑:根据这里找到的答案:在unordered_map中查找值,我会相信这应该是def.

第二次编辑:
这是returnTypeMap()函数:

std::unordered_map <std::string, std::pair<std::string, std::string>> returnTypeMap()
{
      return typeTable;
}
Run Code Online (Sandbox Code Playgroud)

这是我的地图的定义:

std::unordered_map <std::string, std::pair<std::string, std::string>> typeTable;
Run Code Online (Sandbox Code Playgroud)

Man*_*rse 5

您将返回mapby值,因此每个调用的计算结果完全不同map.进入不同容器的迭代器不兼容,并且尝试比较它们具有未定义的行为.

尝试更改代码以通过const引用返回:

std::unordered_map<std::string, std::pair<std::string, std::string>> const& 
returnTypeMap() const
{
    return typeTable;
}
Run Code Online (Sandbox Code Playgroud)

或制作地图的本地副本并致电findend在单个本地副本上:

auto typeTable{p_Repos->returnTypeMap()};
if (typeTable.find(tc[0]) != typeTable.end()) {
    //...
}
Run Code Online (Sandbox Code Playgroud)