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] =
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.