How can I use a custom type as key for a map in C++?

后端 未结 3 478
Happy的楠姐
Happy的楠姐 2020-12-01 12:25

I am trying to assign a custom type as a key for std::map. Here is the type which I am using as key:

struct Foo
{
    Foo(std::string s) : foo_va         


        
3条回答
  •  醉梦人生
    2020-12-01 13:05

    It's probably looking for const member operators (whatever the correct name is). This works (note const):

    bool operator<(const Foo& foo1) const { return foo_value < foo1.foo_value;}
    

    EDIT: deleted operator> from my answer as it was not needed (copy/paste from question) but it was attracting comments :)

    Note: I'm 100% sure that you need that const because I compiled the example.

提交回复
热议问题