C++

Asked • 07/03/19

Is there any advantage of using map over unordered_map in case of trivial keys?

A recent talk about `unordered_map` in C++ made me realize, that I should use `unordered_map` for most cases where I used `map` before, because of the efficiency of lookup ( *amortized O(1)* vs. *O(log n)* ). Most times I use a map I use either `int`'s or `std::strings` as keys, hence I've got no problems with the definition of the hash function. The more I thought about it, the more I came to realize that I can't find any reason of using a `std::map` in case of simple types over a `unordered_map` -- I took a look at the interfaces, and didn't find any significant differences that would impact my code. Hence the question - is there any real reason to use `std::map` over `unordered map` in case of simple types like `int` and `std::string`? I'm asking from a strictly programming point of view -- I know that it's not fully considered standard, and that it may pose problems with porting. Also I expect that one of the correct answers might be *"it's more efficient for smaller sets of data"* because of a smaller overhead (is that true?) -- hence I'd like to restrict the question to cases where the amount of keys is non-trivial (>1 024). **Edit:** *duh, I forgot the obvious (thanks GMan!) -- yes, map's are ordered of course -- I know that, and am looking for other reasons.*

1 Expert Answer

By:

Corbin P. answered • 08/22/20

Tutor
5 (9)

4th Year Computer Engineering Student

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.