mardi 21 juillet 2015

Sorting unordered_map by value

I have an unordered_map<string, int> that I would like to sort. I understand that the unordered_map does not use position to lookup elements and I want to find the elements with greatest value(int).

std::unordered_map

Associative: Elements in associative containers are referenced by their key and not by their absolute position in the container.

Unordered: Unordered containers organize their elements using hash tables that allow for fast access to elements by their key.

I could create a list with the contents of the unordered_map and sort it, and then repopulate the unordered_map with the sorted list.

However I would like to save space and sort the unordered_map directly, what is the syntax, if possible?

Aucun commentaire:

Enregistrer un commentaire