Click here to Skip to main content
16,021,560 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to understand that major difference between map and unordered_map in C++.


which one i suppose to use and which one is best?
Posted

An ordered map is usually implemented on the basis of an ordered tree, for example red-black-tree in STL's case. Tree manipulations are relatively expensive, but the tree uses very little storage.

An unordered map is usually implemented as a hash table; the newer STL implementation offer such a map as well. Hash table are very fast, but have some storage overhead and can only grew to a certain number of nodes before they become inefficient.

So if you have a set of values for which you can estimate the maximum number of nodes and for which you don't need to iterate through the nodes in an ordered fashion, the unordered map is usually the best choice. In all other cases you better use an ordered (tree-based) map.

[AMENDMENT - A comment by pasztorpisti]

Depending on the scenario and the allocators in use trees can extremely fragment memory while a simple yet effective hashmap is only 2 arrays. Most of the time I use only hashmaps and a special kind of ordered map (built on top of an orderedset) that is an array that is always sorted. Depending on the frequency of different operations and the number of items trees may become the better solution but in my opinion that is relatively rare.
 
Share this answer
 
v2
Comments
CPallini 15-Nov-13 3:19am    
5.
pasztorpisti 17-Nov-13 8:29am    
+5, but be careful saying that "the tree uses very little storage". Depending on the scenario and the allocators in use trees can extremely fragment memory while a simple yet effective hashmap is only 2 arrays. Most of the time I use only hashmaps and a special kind of ordered map (built on top of an orderedset) that is an array that is always sorted. Depending on the frequency of different operations and the number of items trees may become the better solution but in my opinion that is relatively rare.
nv3 17-Nov-13 8:57am    
I can fully agree what you said and have the same tendency of using hashmaps whenever possible. May I take you comment and add it literaly to the main entry?
pasztorpisti 17-Nov-13 9:21am    
Of course. :-)
What't the difference between using Google[^] or not?
 
Share this answer
 
Comments
ranjithkumar81 15-Nov-13 3:47am    
hello, i could not find the proper answer in Google as per my search.
Hi,
map:

- Usually implemented using red-black tree.
- Elements are sorted.
- Relatively small memory usage (doesn't need additional memory for the hash-table).
- Relatively fast lookup: O(log N).

unordered_map:

- Usually implemented using hash table.
- Elements are not sorted.
- Requires additional memory to keep the hash-table.
- Fast lookup O(1), but constant time depends on the hash-function which could be relatively slow.

refrence: (http://learninger.com[^])
 
Share this answer
 
v2
None of these is "best". All of these data structures have their own characteristics. These characteristics are often described by listing the time complexity of the operations (insert, remove, add, ...) provided by the data structure. Here is a relatively brief yet good list of such a list: http://stackoverflow.com/questions/7294634/what-are-the-time-complexities-of-various-data-structures[^]

Each data structure has its own advantages and disadvantages, there is no perfect. Which data structure to use? Analyze the activity of your program, which operations does it use often? Choose a data structure whose advantages include most of the operations you often use. It will have a few slow operations but you know that you won't use those often...

Sometimes reality is different than what you would expect. Often data structures built from arrays are much faster if constructed/organized in a smart way because of better locality / cache-friendliness but this is also a hardware dependent thing (I referred to today's average desktop computers).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900