所以我有一个 std::unordered_map,我想访问存储在该映射中的字符串。我想搜索 map 内的所有单词并与给定单词进行比较。如果字符串相同则继续执行 if 语句。
{
public:
bool CheckFoo(const char* word);
protected:
typedef std::unordered_map<std::string, bool> word_map;
word_map words_map;
};
bool CheckFoo(const char* word)
{
if (words_map.empty())
{
return false;
}
auto it = words_map.begin();
while (it != words_map.end())
{
const std::string &r = it->first;
const char* tmp = word;
if (strcmp(tmp, r.c_str() ) == 0)
{
return true;
}
}
return false;
}
if ( CheckFoo("wordFoo") )
{
// bla bla
}
问题是这些代码生成了 .core 转储文件。 你发现我的代码有什么错误吗?
崩溃核心分析将我指向 strcmp 行
最佳答案
还不能写评论,但是,
就像 Nunchy 所写的那样,tmp 没有在该上下文中定义。 我还注意到您的代码永远不会增加 map 迭代器,这将导致永无止境的循环。
我假设您没有将实际代码复制到帖子中,而是匆忙重写了它,这导致了一些拼写错误,但如果没有,请尝试确保您使用的是 temp 而不是 < em>tmp 在调用 strcmp 时,并确保循环实际上递增迭代器。
就像您帖子中的评论之一所指出的那样,请确保 map 和函数参数中确实有数据。
关于c - Strcmp 生成核心转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53422192/