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

前端 未结 5 1207
暗喜
暗喜 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:56

    You can use std::set instead of std::map.

    You can store both key and value in std::pair and the type of container will look like this:

    std::set< std::pair > items;
    

    std::set will sort it's values both by original keys and values that were stored in std::map.

提交回复
热议问题