How can I sort an STL map by value?

前端 未结 8 2235
陌清茗
陌清茗 2020-11-27 04:09

How can I implement STL map sorting by value?

For example, I have a map m:

map m;
m[1] = 10;
m[2] = 5;
m[4] = 6;
m[6] =          


        
8条回答
  •  被撕碎了的回忆
    2020-11-27 04:22

    You can build a second map, with the first map's values as keys and the first map's keys as values.

    This works only if all values are distinct. If you cannot assume this, then you need to build a multimap instead of a map.

提交回复
热议问题