Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

c++ - Cant insert to std::map (G++)

I have a following trouble:

struct ServerPP {
    std::string name;
    int id;
    int expires;
};
std::map<std::string, std::set<ServerPP>> RemindTable;

int test(std::string email, ServerPP serv)
{
    RemindTable[email].insert(serv); // error when compile in this row below
}

Error in g++:

In file included from /usr/include/c++/4.4/string:50,
                 from /usr/include/c++/4.4/bits/locale_classes.h:42,
                 from /usr/include/c++/4.4/bits/ios_base.h:43,
                 from /usr/include/c++/4.4/ios:43,
                 from /usr/include/c++/4.4/istream:40,
                 from /usr/include/c++/4.4/sstream:39,
                 from stdafx.h:19,
                 from ActiveReminder.cpp:4:
/usr/include/c++/4.4/bits/stl_function.h: In member function 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = ServerPP]':
/usr/include/c++/4.4/bits/stl_tree.h:1170:   instantiated from 'std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = ServerPP, _Val = ServerPP, _KeyOfValue = std::_Identity<ServerPP>, _Compare = std::less<ServerPP>, _Alloc = std::allocator<ServerPP>]'
/usr/include/c++/4.4/bits/stl_set.h:411:   instantiated from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const _Key&) [with _Key = ServerPP, _Compare = std::less<ServerPP>, _Alloc = std::allocator<ServerPP>]'
ActiveReminder.cpp:32:   instantiated from here
/usr/include/c++/4.4/bits/stl_function.h:230: error: no match for 'operator<' in '__x < __y'

How can i fix this error in G++, on windows all ok

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to define operator < for your ServerPP data structure if you want to be able to use it in std::set. For instance:

bool operator < (ServerPP const& lhs, ServerPP const& rhs)
{
    return (lhs.id < rhs.id);
}

Alternatively, you can define your own comparator and provide its type as the second template argument to std::set:

struct serv_comp
{
    bool operator () (ServerPP const& lhs, ServerPP const& rhs)
    {
        return (lhs.id < rhs.id);
    }
};

std::map<std::string, std::set<ServerPP, serv_comp>> RemindTable;

Here is a live example showing the code compiling.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...