Linux使用hash_map遇到问题

zmzbs123 2011-04-26 08:38:08
#include <ext/hash_map>
using namespace __gnu_cxx;
class str_hash{
public:
size_t operator()(const string& str) const
{
unsigned long __h = 0;
for (size_t i = 0 ; i < str.size() ; i ++)
__h = 5*__h + str[i];
return size_t(__h);
}
};

class str_compare
{
public:
bool operator()(const string& str1,const string& str2)const
{return str1==str2;}
};
int main()
{

hash_map<string,string,str_hash,str_compare>::iterator iter=nouns.find(name);
if (iter == nouns.end())
{
cout<<iter->first<<endl;////每次都core在这里
}
}

每次都core的地方:
0x004cc43c in std::operator<< <char, std::char_traits<char>, std::allocator<char> > () from /usr/lib/libstdc++.so.6

各位达人,请问这是神马原因呢?



...全文
272 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengzhixi 2011-04-27
  • 打赏
  • 举报
回复
if (iter == nouns.end())
对于iter为end时,通过iter的任何访问都是未定义的行为
kingstarer 2011-04-27
  • 打赏
  • 举报
回复
if (iter == nouns.end())
{
cout<<iter->first<<endl;////每次都core在这里
}

应该是

if (iter != nouns.end()) //找到才打印
{
cout<<iter->first<<endl;////每次都core在这里
}

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧