Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting std::unordered_map by key

Tags:

How can I sort an unordered_map by key? I need to print an unordered_map sorted by key.

like image 385
softwarematter Avatar asked Jun 02 '11 09:06

softwarematter


People also ask

Are Keys sorted in unordered_map?

An unordered_map is a hash container, that is, the keys are hashed. Inside of the container, they don't have the same representation as on the outside. Even the name implies that you can't sort it.

Are map keys sorted C++?

By default, a Map in C++ is sorted in increasing order based on its key.

Which is faster ordered map or unordered_map?

If you use more modern Studio like 2017 - then unordered_map much faster than ordered map.


1 Answers

std::unordered_map<int, int> unordered;  std::map<int, int> ordered(unordered.begin(), unordered.end()); for(auto it = ordered.begin(); it != ordered.end(); ++it)      std::cout << it->second; 
like image 68
ronag Avatar answered Sep 22 '22 05:09

ronag