I could easily do this with a very cheesy method (sorting a vector of pairs by value), but is there a way to sort a map by value, not by key? I can't seem to find any way to do this.
phridge: How would a multimap help? It's still "sorted" by key, isn't it?
If you really need access to the sorted data often, as well the mapped data, then store only pointers to the values in both a map and a set. You might consider a wrapper class to handle all this.
Yes. There is definitly a way to sort a map by the value rather than the key. However, since I'm on vacation, I don't have access to my CUJ that describes how to do it. I'll post a follow up once I get back (in 10 days or so) with an example of how to do it. This method does not make use of a secondary container (like a vector). The article in question is a monthly column called "The Standard Librarian" written by Matt Austern. I cannot remember which issue talked about sorting a map by value.
I was thinking that you could make your values the keys and vice versa. I chose a multimap figuring that your values may not have been unique, so if you used them as keys the map would need to be able to accomodate this.
Perhaps a better option is to use a set instead. Just put your key and value in a struct instead of as a std:air, like a map does. Then you could overload the less than operator and sort the data anyway you want.
Perhaps a better option is to use a set instead. Just put your key and value in a struct instead of as a std:air, like a map does. Then you could overload the less than operator and sort the data anyway you want.