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

前端 未结 1 1774
面向向阳花
面向向阳花 2020-12-21 08:20

I have a following trouble:

struct ServerPP {
    std::string name;
    int id;
    int expires;
};
std::map> Rem         


        
相关标签:
1条回答
  • 2020-12-21 08:42

    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.

    0 讨论(0)
提交回复
热议问题