UNORDERED MULTIMAP in Cpp

UNORDERED MULTIMAP in Cpp

INTRODUCTION

UNORDERED MULTIMAP in Cpp

In this tutorial, we are going to learn about the unordered multimap container in the standard template library of C++ programming language. An unordered multimap is an associative container that stores items consisting of key-value pairs. The unordered multimap is almost similar to an unordered_map except for permitting the repeated values.

The syntax of the unordered multimap is as follows

Unordered_multimap< key_datatype , mapped_datatype > var_name;

Where key_datatype and mapped_datatype represent the data types of the key_value and the mapped_value respectively.

Let’s look at some of the properties of the unordered_multimap.

  • The unordered_multimap store non-unique key-value pairs.
  • There is no specific order in storing the items in the unordered_multimap.
  • The unordered_multimap implements the hash table.
  • The items with the same key in the unordered_multimap are grouped together in the same bucket.

These are some of the properties of the unordered_multimap.

Let’s look at some of the basic methods that are performed on the unordered_multimaps.

insert():  this method adds a new item to the unordered_multimap in a specific order

erase():  this method removes the particular item from the unordered_multimap.

begin(): this method returns an iterator to the first element in the unordered_multimap.

end(): this method returns an iterator to the last element in the unordered_multimap.

count(): this method is used to return the number of items stored in the unordered_multimap.

empty():  this method returns a boolean value whether the particular unordered_multimap is empty or not.

clear(): this method is used to remove all the elements from the unordered_multimap and empty it.

See also  C++ Vectors

These are some of the basic methods that are implemented on the unordered_multimap container.

Let’s look at a sample program implementing unordered_multimaps

#include <iostream>
#include <string>
#include <unordered_map> 
using namespace std;
int main() {  
unordered_multimap <string, int> m1;

m1 = { {"stephen",98} , {"alice",89}};
m1.insert ( {{"robert",77} , {"william",100}} );
pair < string, int> hit2 ("steve",95);
m1.insert (hit2);
m1.insert (make_pair <string, int> ("steve",91));
cout << "Unordered multimap contains: " << endl;
unordered_multimap <string, int> :: iterator itr;
for (itr= m1.begin(); itr!= m1.end(); ++itr) {
cout<<(*itr).first<<"\t"<<(*itr).second<<endl;
} 

cout << "\nErasing first pair " << endl;
m1.erase (m1.begin());

for (itr= m1.begin(); itr!= m1.end(); ++itr) {
cout<<(*itr).first<<"\t"<<(*itr).second<<endl;
}

cout<<"\nsize of the unordered_multimap is : \n"<<m1.size()<<endl;

m1.clear();
cout << "\nChecking the state of the Unordered multimap:" << endl;
if (m1.empty()) 
  cout << "Unordered multi map is empty " << endl;
else 
  cout << "Unordered multi map is not empty " << endl;
  return 0;
}

The output of the program is.

In the above program,

  • we created a unordered_multimap and inserted the items consisting of string keys and integer-mapped values by using different methods.
  • After inserting the items into the unordered_multimap, we created an iterator to traverse over the unordered_multimap.
  • We removed an item from the unordered_multimap by using the erase() method. We calculated the number of elements in the unordered_multimap by using the size() method.
  • After implementing all the basic operations, we removed all the items from the unordered_multimap by using the clear() method.

This is the implementation of the unordered_multimap.

CONCLUSION

That’s it from this tutorial. We have learned about the unordered_multimap container, its properties, basic methods that are implemented on unordered_multimaps and differences between the unordered_multimaps and the multimaps. Hope you guys found it interesting. Happy Coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.

Powered By
100% Free SEO Tools - Tool Kits PRO