map vs unordered_map in C++
Pre-requisite : std::map, std::unordered_map
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.
Difference :
| 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.
// CPP program to demonstrate use of std::map #include <bits/stdc++.h> int main() { // Ordered map std::map<int, int> order; // Mapping values to keys order[5] = 10; order[3] = 5; order[20] = 100; order[1] = 1; // Iterating the map and printing ordered values for (auto i = order.begin(); i != order.end(); i++) { std::cout << i->first << " : " << i->second << '\n'; } } |
chevron_right
filter_none
Output :
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.
// CPP program to demonstrate use of // std::unordered_map #include <bits/stdc++.h> int main() { // Unordered map std::unordered_map<int, int> order; // Mapping values to keys order[5] = 10; order[3] = 5; order[20] = 100; order[1] = 1; // Iterating the map and printing unordered values for (auto i = order.begin(); i != order.end(); i++) { std::cout << i->first << " : " << i->second << '\n'; } } |
chevron_right
filter_none
Output :
1 : 1 3 : 5 20 : 100 5 : 10
Recommended Posts:
- Difference Between Web server and Application server
- Structures in C++
- Variables in C++
- Implementing Forward Iterator in BST
- Count substrings that contain all vowels | SET 2
- Difference between ISO9000 and SEI-CMM
- Difference between Management Information System (MIS) and Decision Support System (DSS)
- Machine Learning in C++
- How can we use Comma operator in place of curly braces?
- Optimally accommodate 0s and 1s from a Binary String into K buckets
- Constants vs Variables in C language
- Difference between LONGs vs LOBs
- Difference between for and do-while loop in C, C++, Java
- Difference between for and while loop in C, C++, Java
- Remove first adjacent pairs of similar characters until possible
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.



