对象作为无序映射的键
我在将类的对象作为键放入无序映射中时遇到问题 这是一个简单的例子:
class first
{
string name;
public:
first(){}
first(string nam):name(nam){}
string get_name() const
{
return name;
}
};
struct SampleTraits
{
size_t operator()(const first &that) const
{
return tr1::hash<const char*>()(that.get_name().c_str());
}
bool operator()(const first &t1,const first &t2) const
{
return t1.get_name()==t2.get_name();
}
};
typedef tr1::unordered_set<unsigned short> uset;
typedef tr1::unordered_map<first,uset,SampleTraits,SampleTraits> umap;
ostream& operator <<(ostream& out, uset &ust)
{
for(uset::iterator it=ust.begin();it!=ust.end();++it)
out<<" "<<*it;
}
int main()
{
umap *mymap= new umap;
string names,nm,n;
cout<<"\nEnter 1st name: ";
cin>>names;
first obj(names);
(*mymap)[obj].insert(100);
(*mymap)[obj].insert(120);
(*mymap)[obj].insert(112);
cout<<"\nEnter 2nd name:";
cin>>nm;
first obj2(nm);
(*mymap)[obj2].insert(201);
(*mymap)[obj2].insert(202);
cout<<"\nEnter name which u want to search:";
cin>>n;
first obj1(n);
umap::iterator it=mymap->find(obj1);
cout<<it->first.get_name();
cout<<it->second;
//delete mymap;
/*
for(umap::iterator it=mymap->begin();it!=mymap->end();it++)
{
cout<<it->first.get_name()<<" ";
cout<<it->second<<endl;
}
*/
return 0;
}
我的问题是当我尝试插入两个不同的对象并尝试显示它时显示分段错误..再次如果我尝试使用 find() 那么它也会显示分段错误..这对我来说相当困难了解为什么 unordered_map 显示此行为。
任何帮助将不胜感激!这将对我的项目有很大帮助......
I am having problem in putting object of a class in an unordered map as key
here is a simple example:
class first
{
string name;
public:
first(){}
first(string nam):name(nam){}
string get_name() const
{
return name;
}
};
struct SampleTraits
{
size_t operator()(const first &that) const
{
return tr1::hash<const char*>()(that.get_name().c_str());
}
bool operator()(const first &t1,const first &t2) const
{
return t1.get_name()==t2.get_name();
}
};
typedef tr1::unordered_set<unsigned short> uset;
typedef tr1::unordered_map<first,uset,SampleTraits,SampleTraits> umap;
ostream& operator <<(ostream& out, uset &ust)
{
for(uset::iterator it=ust.begin();it!=ust.end();++it)
out<<" "<<*it;
}
int main()
{
umap *mymap= new umap;
string names,nm,n;
cout<<"\nEnter 1st name: ";
cin>>names;
first obj(names);
(*mymap)[obj].insert(100);
(*mymap)[obj].insert(120);
(*mymap)[obj].insert(112);
cout<<"\nEnter 2nd name:";
cin>>nm;
first obj2(nm);
(*mymap)[obj2].insert(201);
(*mymap)[obj2].insert(202);
cout<<"\nEnter name which u want to search:";
cin>>n;
first obj1(n);
umap::iterator it=mymap->find(obj1);
cout<<it->first.get_name();
cout<<it->second;
//delete mymap;
/*
for(umap::iterator it=mymap->begin();it!=mymap->end();it++)
{
cout<<it->first.get_name()<<" ";
cout<<it->second<<endl;
}
*/
return 0;
}
My problem is when iam tryin to insert two different objects and trying to display it is shows segmentation fault.. again if i try to use find() then also it shows segmentation fault.. Its quite hard for me to understand why unordered_map is showing this behavior.
Any help will be appreciated!! This will be a great help for my project...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题出在哈希函数上。它不能像您预期的指针类型那样工作,因为它使用指针来计算哈希值而不是其内容。使用 std::string 可以解决问题。
The problem is with hash function. It does not work as you have expected with pointer types, since it uses a pointer to calculate a hash value instead of its content. Using the std::string fixes the problem.
看起来您忘记从操作员返回<<用于用户。尽管大多数编译器都会对此类函数发出警告,但它们仍然必须编译它们,并且运行此类程序将导致未定义的行为。
Looks like you forgot to return out from operator<< for the uset. Although, most compilers will issue warning for such functions they still have to compile them and running such program will result in undefined behaviour.