An associative container is an unordered multimap . It stores key-value pairs similarly to how an unordered map does. On the other hand, repeated values are acceptable in multimaps but not in unordered maps. These are unordered containers, so there is no order in the process of storing stuff. Though, the duplicates are permitted so things with the same key are gathered in the same bucket. For duplicate keys, each key-value pair has a unique count value.
The internal hash algorithm establishes the temporal complexity of unordered multimaps, which store key-value pairs in a hash table. Any operation takes a constant amount of time on average, and a linear amount of time on worst.
Syntax:
The syntax for unordered_multimap in the C++ STL is as follows.
unordered_multimap<key_datatype, mapped_datatype> map_name;
Example:
Let's take an example to illustrate the use of unordered_multimap function in C++.
#include <bits/stdc++.h>
using namespace std;
int main(void) {
// Create an unordered_multimap of string to int
unordered_multimap<string, int> ummp;
// Insert key-value pairs into the unordered_multimap
ummp.insert({"apple", 3});
ummp.insert({"banana", 2});
ummp.insert({"cherry", 5});
ummp.insert({"date", 1});
ummp.insert({"apple", 4}); // Note that "apple" is duplicated
cout << "Unordered multimap contains: " << endl;
// Iterating over the unordered_multimap
for (auto it = ummp.begin(); it != ummp.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
Output:
Unordered multimap contains:
apple = 3
apple = 4
banana = 2
cherry = 5
date = 1
Methods of Unordered Multimap:
Below is a list of some of the unordered_multimap's often used functions:
begin -
It returns an iterator pointing to the first element of either the container or one of its buckets.
end -
It provides an iterator thacpp tutorials to the location following the final element of either the container or one of its buckets.
count -
It returns the number of container items whose key corresponds to the key of the parameter.
cbegin -
It returns a constant iterator thacpp tutorials to the first element of either the container or one of its buckets.
cend -
It provides a constant iterator thacpp tutorials to the position following the final element of either the container or one of its buckets.
clear -
It clears the contents of the unordered multimap container.
size -
It returns the size of the unordered multimap. It shows the quantity of goods inside the container.
swap -
It switches the contents of two unordered multimap containers. The two containers may have different sizes.
find -
It returns an iterator pointing to an entry with the key k; bucket size - Returns the number of elements in the bucket n.
find -
This function returns true if the unordered multimap container is empty. False is returned if not.
The function max size yields the highest quantity of items that the unordered multimap container is capable of holding.
emplace -
It adds a new key element to the unordered multimap container.
Example:
In the example below, a few of these techniques are being used.
#include <bits/stdc++.h>
using namespace std;
// Making a typedef for short declaration
typedef unordered_multimap<string, int>::iterator unit;
// Function to print the unordered_multimap
void printing_map(unordered_multimap<string, int> ummp)
{
// Iterating over the unordered_multimap
for (auto it = ummp.begin(); it != ummp.end(); ++it)
cout << it->first << " = " << it->second << endl;
cout << endl;
}
int main()
{
// Initialization
unordered_multimap<string, int> mp2(
{
{ "apple", 5 },
{ "banana", 3 },
{ "cherry", 8 },
{ "date", 2 },
{ "apple", 4 },
{ "cherry", 6 },
{ "banana", 4 }
}
);
cout << "Initial unordered multimap:\n";
printing_map(mp2);
// Total number of elements in the unordered_multimap
cout << "Size of the unordered multimap is " << mp2.size() << endl;
string key = "banana";
// Finding the first pair associated with the key "banana"
unit it = mp2.find(key);
if (it != mp2.end()) {
cout << "Key " << key << " is there in the unordered multimap\n";
cout << "Value associated with " << key << " is " << it->second << endl;
}
else
cout << "Key " << key << " is not present\n" << endl;
// Count the number of pairs with the key "banana"
int cnt = mp2.count(key);
cout << "Number of values with " << key << " are " << cnt << "\n";
// Insertion by making pair explicitly
mp2.insert(make_pair("date", 6));
// Insertion by initializer list
mp2.insert({{ "fig", 7 }, { "grape", 9 }});
cout << "\nAfter insertion:\n";
printing_map(mp2);
// Erase deletes all pairs corresponding to the key "apple"
mp2.erase("apple");
cout << "After deletion of apple:\n";
printing_map(mp2);
// Clear deletes all pairs from the container
mp2.clear();
if (mp2.empty())
cout << "unordered multimap is empty";
else
cout << "unordered multimap is not empty";
}
Output:
Initial unordered multimap:
apple = 5
banana = 3
banana = 4
cherry = 8
cherry = 6
date = 2
Size of the unordered multimap is 6
Key banana is there in the unordered multimap
Value associated with banana is 3
Number of values with banana are 2
After insertion:
apple = 5
banana = 3
banana = 4
cherry = 8
cherry = 6
date = 2
date = 6
fig = 7
grape = 9
After deletion of apple:
banana = 3
banana = 4
cherry = 8
cherry = 6
date = 2
date = 6
fig = 7
grape = 9
unordered multimap is empty
Time Complexity:
Depending on the internal hash algorithm being utilized, all operations on an unordered multimap typically require the same length of time. In the worst case, time can grow to linearity. But multimap (tree-based multimap) is inferior to unordered multimap.
In the usual case, linear, that is, O(n) .
In the worst case, O(n^2) , or quadratic.