When it comes to efficiency, there is a huge difference between maps and unordered maps.
We must know the internal working of both to decide which one is to be used.
| map | unordered_map --------------------------------------------------------- Ordering | increasing order | no ordering | (by default) | Implementation | Self balancing BST | Hash Table | like Red-Black Tree | search time | log(n) | O(1) -> Average | | O(n) -> Worst Case Insertion time | log(n) + Rebalance | Same as search Deletion time | log(n) + Rebalance | Same as search
Use std::map when
- You need ordered data.
- You would have to print/access the data (in sorted order).
- You need predecessor/successor of elements.
- See advantages of BST over Hash Table for more cases.
1 : 1 3 : 5 5 : 10 20 : 100
Use std::unordered_map when
- You need to keep count of some data (Example – strings) and no ordering is required.
- You need single element access i.e. no traversal.
1 : 1 3 : 5 20 : 100 5 : 10
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count of elements which cannot form any pair whose sum is power of 2
- Array sum after replacing all occurrences of X by Y for Q queries
- Spring vs. Struts in Java
- Difference between Java and Core Java
- Why do we need reference variables if we have pointers
- Cohen-Sutherland vs. Liang-Barsky line clipping algorithm
- Difference between Trafodion and XAP
- Difference between Project and Product
- Difference between Blog and Vlog
- Difference between Elasticsearch and MongoDB
- Difference between Google Hangouts and Google Duo
- Difference between Texting and Email
- Difference between AgensGraph and AnzoGraph
- Difference between VelocityDB and Warp 10
- Difference between VelocityDB and Weaviate
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.