How can I use std::maps with user-defined types as key?

前端 未结 7 1689
灰色年华
灰色年华 2020-11-22 04:30

I\'m wondering why I can\'t use STL maps with user-defined classes. When I compile the code below, I get the following cryptic error message. What does it mean? Also, why is

7条回答
  •  醉酒成梦
    2020-11-22 05:03

    class key
    {
        int m_value;
    public:
        bool operator<(const key& src)const
        {
            return (this->m_value < src.m_value);
        }
    
    };
    int main()
    {
        key key1;
        key key2;
        map mymap;
        mymap.insert(pair(key1,100));
        mymap.insert(pair(key2,200));
        map::iterator iter=mymap.begin();
        for(;iter!=mymap.end();++iter)
        {
            cout<second<

提交回复
热议问题