![]() |
VOOZH | about |
In C++, the unordered_multimap is an unordered associative container that stores data in the form of key-value pairs. It is similar to unordered map, but it allows multiple elements with the same key. It provides fast insertion, deletion and search operations in O(1) time by using hashing.
Example:
3: C++ 2: For 1: STL 1: Geeks
Explanation: In this example, we create an unordered multimap umm with four key value pairs: {1, "Geeks"}, {2, "For"}, {3, "C++"} and {1, "STL"}. We can see from the output that there is no order of the elements.
Unordered map is defined as the std::unordered_multimap class template inside the <unordered_map> header file.
unordered_multimap<key_type, value_type, > umm;
where,
Unordered multimap can be declared and initialized in multiple ways as shown in the below example:
1: C++ 1: Geeks 2: For
Explanation: In the above example,
The basic operations on unordered multimap are shown below:
New key-value pairs can be inserted using the insert() function. This function allows inserting multiple elements with the same key. This map container does not support the use of [] operator because multiple elements with the same key can be present.
Example:
3: C++ 2: Geeks 2: For
Elements can be accessed by its position by moving begin() and end() iterators, but it is not useful. In unordered multimap, accessing value by their key is the primary operation. This can be done by using find() method.
Example:
2: For
In unordered multimap, elements can be accessed by their key using find() method. This method searches for the given key and returns the iterator to the first element with the given key. This iterator can be dereferenced to access the key(first) and value(second) of the pair.
Example:
2: Geeks
Unordered multimap allows users to update the associated value of any key. This can be done by accessing the element and assigning the new value (second member).
Example:
2: Java 2: For 3: C++
Traversing through an unordered multimap can be done using begin() or end() iterators or a range-based for loop.
Example:
1: C++ 1: Geeks 2: For
Explanation: In the above program, traversal through the unordered multimap is done using an iterator with a loop. Starting from beginning, in each iteration, the iterator it points to a pair and then incremented. The traversal stops when it encounters the end() iterator.
Elements can be deleted using erase() by passing either the key or the iterator. If the key is passed, then all the elements with the same key are deleted.
2 For
The below table lists the time complexity of the above operations on unordered multimap:
The unordered multimap is used in various scenarios. The following examples help you master its advanced usage:
An unordered multimap container provides the built-in implementation of a hash table. When an element is inserted, its key is hashed, and the hash value determines the index of the bucket at which the value is stored. Elements with the same hash value (i.e. with same keys) are stored in the same bucket. These buckets are generally implemented as linked lists. This allows for fast access and insertion, basically with constant time complexity O(1) for operations like insert(), erase(), and find(). However, due to hash collisions, the time complexity can degrade to O(n) in the worst case.
The main difference between unordered multimap and unordered map is shown below:
Following is the list of all member functions of std::unordered_multimap class in C++:
Functions | Description |
|---|---|
Returns an iterator pointing to the first element in the unordered multimap. | |
Returns an iterator pointing to the position beyond the last element in the unordered multimap. | |
Return the constant iterator pointing to the first element in the unordered multimap. | |
Returns a constant iterator pointing to the position beyond the last element in the unordered multimap. | |
| clear() | Delete all elements of the unordered multimap. |
| size() | Returns the number of elements present inside the unordered multimap. |
This function is used to swap two unordered multimap. | |
| bucket() | Returns the bucket number where the element with the key k is located in the unordered multimap. |
| bucket_count() | Bucket count is used to count the total no. of buckets in the unordered multimap. No parameter is required to pass into this function |
bucket_size() | Returns the number of elements in each bucket of the unordered multimap. |
max_bucket_count() | Return the maximum number which can hold by a bucket in the unordered multimap. |
Count the number of elements present in an unordered map with a given key | |
Returns iterator to the element with specific key. | |
Checks whether the unordered multimap is empty or not. | |
contains() | Check if the unordered multimap contain an element with specific key. |
Return the maximum number that which can hold by the unordered multimap. | |
Erase elements from the unordered multimap container. | |
Delete all elements from the unordered multimap. | |
This function is used to insert an element into unordered multimap. | |
insert_range() | This function is used to insert range of elements into unordered multimap. |
This function is used to insert an element in the unordered multimap. | |
This function is used to insert a key with his value with a given hint. | |
This function is used to swap two unordered multimaps. | |
extract() | This function is used to extract the node from the unordered multimap. |
merge() | This function is used to merge unordered multimaps into one. |