I am aware that map is not prepared to be sorted, its heavily optimized for fast and random key access., and actually doesn't support std::sort.
My current problem is that I have a full
map<std::string,int>
which I'm not going to use anymore, I just need to extract 10 pairs in value(int) order and destroy it.
The best thing if it was possible would be to sort it in place and then iterate it 10 times, but that apparently is not a solution.
I'm trying different solutions as going through a multimap(to allow duplicate keys) but I'd like to know if there is a more elegant solution, using stl algorithms as much as posible.
EDIT:
I'm using a map because for the 99% of the time I need it as a map, fast key lookups to increase values. Just need a good way of later extracting in value order when I don't need the map anymore.
Current approach whould be:
- std::copy the map (std::string,int) to a vector(pair(std::string,int))
- sort the vector
- get the first 10 values
- destroy vector and map