Open In App

map vs unordered_map in C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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
                | of keys(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




// 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] = 500;
    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';
    }
}


Output

1 : 1
3 : 500
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




// 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] = 500;
    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';
    }
}


Output

1 : 1
20 : 100
3 : 500
5 : 10

Let us see the differences in a tabular form -:

  map unordered_map
1. map is define in  #include <map> header file unordered_map is defined in #include <unordered_map> header file
2. It is implemented by red-black tree. It is implemented using hash table.
3. It is slow. It is fast.
4. Time complexity for operations is O(log N) Time complexity for operations is O(1)
5. map is used to store elements as key,value pairs in order sorted by key. unordered_map is used to store elements as key,value pairs in non-sorted order.


Last Updated : 13 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads