In this instructional exercise, we will find out about stl unordered multimap compartment in C++.
Unordered multimap is a cooperative compartment. Same as unordered guide it stores key and worth pair. In any case, the unordered guide doesn’t permit copy esteems. Here copy esteems are permitted.
Since these are unordered compartments, there is no organization in the method for putting away components.
In any case, this permits copies so the components which have the same key are gathered in a similar can.
For copy keys, another tally esteem is kept up with each key – esteem pair.
Since unordered multimap utilizes a hash table to store key – esteem sets, time multifaceted nature dependent on inner hash work utilized.
t performs consistent time in normal case and in most pessimistic scenario it will require some investment for any activity.
C++ STL Unordered Multimap – std::unordered_multimap
Iterators that can be relevant on unordered multimap:
start(): returns iterator to the start.
end(): returns iterator as far as possible of the holder.
cbegin(): Returns steady iterator to the start.
cend(): Returns consistent iterator as far as possible.
Unordered multimap is only an additional component of an unordered map. So we have to incorporate the same unordered guide header capacity to work with unordered multimap.
i.e #include <unordered_map>
Presently observe a few capacities pertinent on unordered multimap:
embed()
There are various kinds of additions. In any sort, we ought to speak to components in a couple like structure. Since this store’s key worth sets.
i.e { “Key” , “esteem” } group.
Distinctive embeddings ways:
We can straightforwardly instate key – esteem sets setting “=” the image after the presentation.
Utilizing “embed” work additionally we can embed.
We can make pair utilizing sexually transmitted disease:: pair and we can embed this pair into unordered multimap.
Or on the other hand, we can straightforwardly move the pair.
On the off chance that there are other holders which have the key – esteem sets, by determining a range we can duplicate those sets into to unordered multimap.
eradicate()
Same as inclusion, we can perform erasure likewise in various ways.
Erasing by pointing one of the pair utilizing iterator.
Erasing utilizing “key”.
clear(): Clear activity used to evacuate all key worth sets. By result holder gets unfilled.
void(): vacant() is a Boolean capacity. It is utilized to check whether the compartment vacant or not?
Model program to appear above capacities:
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main ()
{
unordered_multimap <string, int> unmmp1;
unordered_multimap <string, int> :: iterator it;
// direct assigning key - value pairs
unmmp1 = { {"Rohit", 264} , {"Guptil" , 237}};
// inserting using "initializor list" function
unmmp1.insert ( {{"Sehwag" , 219} , {"Gayle" , 215}} );
// making pair and inserting
pair < string, int> hit2 ("Rohit" , 209);
unmmp1.insert (hit2);
// moving pair
unmmp1.insert (make_pair <string, int> ("Rohit" , 208));
cout << "Unordered multimap contains: " << endl;
for (it= unmmp1.begin(); it!= unmmp1.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
cout << "Erasing first pair " << endl;
unmmp1.erase (unmmp1.begin());
cout << "Erasing record contains key as \"Guptil\" " << endl;
unmmp1.erase ("Guptil");
cout << "After performing above erase operations...." << endl;
for (it= unmmp1.begin(); it!= unmmp1.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
cout << "Applying clear() operation... " << endl;
unmmp1.clear();
cout << "Checking unordered multi map empty or not " << endl;
bool chk= unmmp1.empty();
if (chk == 1) cout << "Unordered multi map is empty " << endl;
else cout << "Unordered multi map is not empty " << endl;
return 0;
}
Output
Unordered multimap contains:
(Gayle , 215)
(Sehwag , 219)
(Rohit , 208)
(Rohit , 209)
(Rohit , 264)
(Guptil , 237)
Erasing first pair
Erasing record contains key as “Guptil”
After performing above erase operations….
(Sehwag , 219)
(Rohit , 208)
(Rohit , 209)
(Rohit , 264)
Applying clear() operation…
Checking unordered multi map empty or not
Unordered multi map is empty
size(): size() activity used to realize what number of key esteem sets present in the unordered multimap.
max_size(): max_size() returns greatest size of the compartment.
find():
Find is utilized to look through a specific information pair.
We give key as a parameter to this capacity.
On the off chance that key is available iterator indicates it’s position.
Whenever a given key is absent iterator focuses to end of the holder.
tally(): Count utilized will return how often a couple present in the compartment with the given key. We give key as a parameter to check work.
Model program to appear above capacities:
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main ()
{
unordered_multimap <string, int> unmmp1;
unordered_multimap <string, int> :: iterator it;
unmmp1 = { {"Rohit", 264} , {"Guptil" , 237} , {"Rohit", 209} , {"Rohit", 208} };
cout << "Unordered multimap contains: " << endl;
for (it= unmmp1.begin(); it!= unmmp1.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
cout << "Size of the unordered multimap is " ;
cout << unmmp1.size() << endl ;
cout << "Maximum Size of the unordered multimap is " ;
cout << unmmp1.max_size() << endl;
cout << "Finding \"Guptil data \" " << endl;
it = unmmp1.find("Guptil");
if (it!=unmmp1.end())
cout << "Key found and it's value is " << it->second << endl;
else
cout << "Data not found with given key " << endl;
cout << endl << "Counting how many times Rohit key is present " << endl;
cout << "Rohit scored 200+ runs " << unmmp1.count("Rohit") << " times " << endl;
return 0;
}
Output
Unordered multimap contains:
(Guptil , 237)
(Rohit , 208)
(Rohit , 209)
(Rohit , 264)
Size of the unordered multimap is 4
Maximum Size of the unordered multimap is 329406144173384850
Finding “Guptil data ”
Key found and it’s value is 237
Counting how many times Rohit key is present
Rohit scored 200+ runs 3 times
swap(): swap() activity applied on two maps. Bu result all key – esteem sets of unordered multi map1 are moved to unordered multi map2 and bad habit – Versa.
Model program to show swap activity:
#include <iostream>
#include <unordered_map>
using namespace std;
int main ()
{
unordered_multimap <string, int> unmmp1, unmmp2;
unordered_multimap <string, int> :: iterator it;
unmmp1 = { {"Rohit", 264} , {"Guptil" , 237} , {"Sehwag" , 219} , {"Rohit", 209} };
cout << "Unordered multimap :: 1 Before swap: " << endl;
for (it= unmmp1.begin(); it!= unmmp1.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
unmmp2 = { {"Sachin" , 49} , {"Kohli" , 32} , {"Ponting" , 30} , {"JayaSurya" , 28} };
cout << "Unordered multimap :: 2 Before swap: " << endl;
for (it= unmmp2.begin(); it!= unmmp2.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
cout << endl << "Performing swap operation " << endl;
unmmp1.swap(unmmp2);
cout << "Unordered multimap :: 1 After swap: " << endl;
for (it= unmmp1.begin(); it!= unmmp1.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
unmmp2 = { {"Sachin" , 49} , {"Kohli" , 32} , {"Ponting" , 30} , {"JayaSurya" , 28} };
cout << "Unordered multimap :: 2 After swap: " << endl;
for (it= unmmp2.begin(); it!= unmmp2.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
return 0;
}
Output
Unordered multimap :: 1 Before swap:
(Sehwag , 219)
(Guptil , 237)
(Rohit , 209)
(Rohit , 264)
Unordered multimap :: 2 Before swap:
(Ponting , 30)
(Kohli , 32)
(JayaSurya , 28)
(Sachin , 49)
Performing swap operation
Unordered multimap :: 1 After swap:
(Ponting , 30)
(Kohli , 32)
(JayaSurya , 28)
(Sachin , 49)
Unordered multimap :: 2 After swap:
(Ponting , 30)
(Kohli , 32)
(JayaSurya , 28)
(Sachin , 49)