对象作为无序映射的键

发布于 2024-11-02 05:19:07 字数 1980 浏览 1 评论 0原文

我在将类的对象作为键放入无序映射中时遇到问题 这是一个简单的例子:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌逼全场 2024-11-09 05:19:07

问题出在哈希函数上。它不能像您预期的指针类型那样工作,因为它使用指针来计算哈希值而不是其内容。使用 std::string 可以解决问题。

return tr1::hash<string>()(that.get_name());

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.

return tr1::hash<string>()(that.get_name());
叹沉浮 2024-11-09 05:19:07

看起来您忘记从操作员返回<<用于用户。尽管大多数编译器都会对此类函数发出警告,但它们仍然必须编译它们,并且运行此类程序将导致未定义的行为。


ostream& operator <<(ostream & out, uset & ust)
{
    for(uset::iterator it=ust.begin();it!=ust.end();++it)
        out<<" "<<*it;

    return out;
}

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.


ostream& operator <<(ostream & out, uset & ust)
{
    for(uset::iterator it=ust.begin();it!=ust.end();++it)
        out<<" "<<*it;

    return out;
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文