How can I sort a std::map first by value, then by key?

前端 未结 5 1202
暗喜
暗喜 2020-11-27 13:24

I need to sort a std::map by value, then by key. The map contains data like the following:

  1  realistically
  8         really
  4         rea         


        
5条回答
  •  暖寄归人
    2020-11-27 13:46

    std::map already sorts the values using a predicate you define or std::less if you don't provide one. std::set will also store items in order of the of a define comparator. However neither set nor map allow you to have multiple keys. I would suggest defining a std::map if you want to accomplish this using your data structure alone. You should also realize that std::less for string will sort lexicographically not alphabetically.

提交回复
热议问题