How can I sort an STL map by value?

前端 未结 8 2200
陌清茗
陌清茗 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:15

    Dump out all the key-value pairs into a set > first, where the set is constructed with a less-than functor that compares the pair's second value only. That way, your code still works even if your values aren't all distinct.

    Or dump the key-value pairs into a vector >, then sort that vector with the same less-than functor afterwards.

提交回复
热议问题