![]() |
VOOZH | about |
In C++, unordered_map is an associative container that stores unique key–value pairs using hashing, offering fast access. Unlike map, it does not maintain elements in sorted order.
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++"}.
Unordered 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:
3 C++ 1 Geeks 2 For
Explanation: In this example,
The basic operations on unordered map are shown below:
A 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.
3: C++ 2: For 1: Geeks
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.
For Geeks
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.
By Tips
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".
Unordered map provides fast element search by key using the find() member function. This function returns iterator the element if found, otherwise returns end() iterator.
2: For
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.
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.
Elements in an unordered map can be deleted using the erase() function by passing a specific key or the iterator to the element.
1: Geeks
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.
Unordered map is used in many situations for different purposes. The following examples’ aim is to help you master unordered map beyond the basics:
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.
Primary 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. |