How to make a map sort by value C++

后端 未结 5 1223
情话喂你
情话喂你 2021-01-29 08:08

I was trying to make a map sort by value using a custom comparator but I couldn\'t figure out why I kept getting the error of \"no matching call to compareByVal\"

Here\'

5条回答
  •  梦毁少年i
    2021-01-29 08:35

    If you want to sort a std::map by its value, then you are using the wrong container. std::map is sorted by the keys by definition.

    You can wrap key and value:

    struct foo {
        int key;
        int value;
    };
    

    and then use a std::set that uses a comparator that only compares foo::value.

提交回复
热议问题