Last Updated : 23 Jul, 2025
Try it on GfG Practice
In C++, unordered_map is an unordered associative container that stores data in the form of unique key-value pairs. But unlike map, unordered map stores its elements using hashing. This provides average constant-time complexity O(1) for search, insert, and delete operations but the elements are not sorted in any particular order.
Example:
C++
#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
// Creating an unordered_map with integer
// keys and string values
unordered_map<int, string> um =
{{1, "Geeks"}, {2, "For"}, {3, "C++"}};
for (auto i : um)
cout << i.first << ": " << i.second
<< endl;
return 0;
}
3: C++ 1: Geeks 2: For
Explanation: In this example, we created an unordered map um with three key-value pairs: {1, "Geeks"}, {2, "For"} and {3, "C++"}.
SyntaxUnordered map is defined as the std::unordered_map class template inside the <unordered_map> header file.
unordered_map<key_type, value_type> um;
where,
We can declare and initialize unordered map in different ways as shown:
C++
#include <bits/stdc++.h>
using namespace std;
void print(unordered_map<int, string> um){
for (auto i : um)
cout << i.first << " " << i.second
<< endl;
}
int main() {
// Create an empty unordered_map
unordered_map<int, string> um1;
// Creating an unordered_map using
// initializer list
unordered_map<int, string> um2 =
{{1, "Geeks"}, {2, "For"}, {3, "C++"}};
print(um1);
cout << endl;
print(um2);
return 0;
}
3 C++ 1 Geeks 2 For
Explanation: In this example,
The basic operations on unordered map are shown below:
1. Inserting ElementsA new key-value pairs can be inserted in unordered map using either [] operator or insert() method. If the element with the given key already exists, the insert() method skip the insertion but [] operator updates the associated value to the new value.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
unordered_map<int, string> um;
// Insert elements using square brackets
um[1] = "Geeks";
// Insert elements using insert() method
um.insert({2, "For"});
um.insert({3, "C++"});
for (auto i : um)
cout << i.first << ": " << i.second
<< endl;
return 0;
}
3: C++ 2: For 1: Geeks2. Accessing Elements
Elements in unordered map can be accessed using the [] operator or at() function. But if the key is not found, [] operator insert default value for key then return that default value. So, it is better to use at() method.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
unordered_map<int, string> um =
{{1, "Geeks"}, {2, "For"}, {3, "C++"}};
// Access value associated with key 2
// using [] operator
cout << um[2] << endl;
// Access value associated with key 1
// using at() function
cout << um.at(1);
return 0;
}
3. Updating Elements
In unordered map, elements can be updated by simply assigning a new value while accessing using assignment operator. But again with [] operator, a new element will be created if the key is not present.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
unordered_map<int, string> um =
{{1, "Geeks"}, {2, "For"}, {3, "C++"}};
// Updating value associated with key 2
// using [] operator
um[2] = "By";
cout << um[2] << endl;
// Updating value associated with key 1
//using at() function
um.at(1) = "Tips";
cout << um.at(1);
return 0;
}
Explanation: In this example, the [] operator updates the value of key 2 to "By", while the at() function updates the value of key 1 to "Tips".
4. Finding ElementsUnordered map provides fast element search by key using the find() member function. This function returns iterator the element if found, otherwise returns end() iterator.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
unordered_map<int, string> um =
{{1, "Geeks"}, {2, "For"}, {3, "C++"}};
// Finding element with key 2
auto it = um.find(2);
if (it != um.end())
cout << it->first << ": " << it->second;
else cout << "Not Found";
return 0;
}
5. Traversing
Traversing an unordered map involves iterating through all the key-value pairs stored in the container. This can be done by incrementing begin() iterator till it is not equal to end() iterator.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
unordered_map<int, string> um =
{{1, "Geeks"}, {2, "For"}, {3, "C++"}};
// Traversing using iterators with loop
for(auto it = um.begin(); it != um.end(); it++)
cout << it->first << ": " << it->second
<< endl;
return 0;
}
3: C++ 1: Geeks 2: For
Range based for loop can also be used for simple traversal. Since unordered map does not maintain a specific order of elements, the order in which elements are traversed may not match the order in which they were inserted.
6. Deleting ElementsElements in an unordered map can be deleted using the erase() function by passing a specific key or the iterator to the element.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
unordered_map<int, string> um =
{{1, "Geeks"}, {2, "For"}, {3, "C++"}};
// Delete element which have key 2
um.erase(2);
// Delete first element
um.erase(um.begin());
for(auto it = um.begin(); it != um.end(); it++)
cout << it->first << ": " << it->second
<< endl;
return 0;
}
Explanation: In the above example, delete the pair that has the key 2 and also delete the first element pointed to by the iterator begin() using the erase() method.
Time ComplexityThe below table lists the time complexity of the above operations on unordered map:
Operation Time Complexity Insert an element O(1) (average) Delete an element by key O(1) (average)Access element by key
O(1) (average)
Find element by key O(1) (average) Update element by key O(1) (average) Traverse the map O(n) Other Common OperationsUnordered map is used in many situations for different purposes. The following examples’ aim is to help you master unordered map beyond the basics:
Internal Working
In C++, unordered map provides the built-in implementation of hash table data structure. It hashes only the key from each element to find its index in the table and stores the element as key value pair. As it uses hashing, insertion, deletion and search operations take O(1) amortized time.
Unordered Map vs MapPrimary difference between unordered map and map is shown below:
Following is the list of all member functions of std::unordered_map class in C++:
Functions
Description
at() This function in C++ unordered map returns the reference to the value with the element as key k.operator []
This operator is used to access or update the value of a specific key.
contains()
This function is used to check if the container contains element with specific key.
Return the bounds of a range that includes all the elements in the container with a key that compares equal to k
begin() Returns an iterator pointing to the first element in the container. end() Returns an iterator pointing to the position beyond the last element in the container.Return the constant iterator pointing to the first element in the container.
Returns a constant iterator pointing to the position beyond the last element in the container.
bucket() Returns the bucket number where the element with the key k is located in the unordered map. bucket_count() Bucket count is used to count the total no. of buckets in the unordered map. No parameter is required to pass into this function bucket_size() Returns the number of elements in each bucket of the unordered map.Return the maximum number which can hold by a bucket in the container.
count() Count the number of elements present in an unordered map with a given key. find() Returns iterator to the element with specific key. empty() Checks whether the unordered map is empty or not.Return the number of elements present inside the unordered map.
Return the maximum number that which can hold by the unordered map.
erase() Erase elements from the unordered map container.Delete all elements from the unordered map.
This function is used to insert an element into unordered map.
insert_range()
This function is used to insert range of elements into unordered map.
This function is used to insert an element in the unordered map.
This function is used to insert a key with his value with a given hint.
This function is used to swap two unordered maps.
extract()
This function is used to extract the node from the unordered map.
merge()
This function is used to merge unordered maps into one.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4