C++ STL Multimap Container – std::multimap

In this tutorial you will learn about stl multimap i.e., std::multimap and all functions applicable on it with some example code.

In previous articles we already learned about std::map container. It is an associative container which give priority to key values. But problem with map is it won’t allow duplicate values. But multimap allows duplicate values. Here in multimap both key and value pair becomes unique. And we already know that one of the great property of map and multimap is always elements automatically inserted into sorted order on fly. Keys can’t be modified once inserted. Only possible way is we have to delete and update with a new value.

Implementation of multimaps follows a binary search tree type of implementation.

C++ STL Multimap Container – std::multimap

To work with multimap we need to include map header file.

#include <map>

Iterators that can be applicable on multimap:

begin(): returns iterator to the beginning.

end(): returns iterator to the end of the map.

rbegin(): returns reverse iterator to reverse beginning.

rend(): returns reverse iterator to reverse end.

cbegin(): Returns constant iterator to the beginning.

cend(): Returns constant iterator to the end.

Declaring a multimap:

multimap < datatype, datatype > multiMapName;

This is for key and value pair.

Now see some basic operations on multimap:

Insert operation: Insert operations effects size of multimap.

multiMapName.insert (pair (datatype, datatype ) ( keyt, value ))

Another type of insertion is, we can directly assign one multimap elements to other multimap elements directly by giving range using iterators.

multimap <datatype, datatype> newMmap (oldMmap.begin(), oldMmap.end())

size(): This will return the number of key value pairs in the multimap.

max_size(): This will return the what is the maximum capacity of the multimap

clear(): This will truncate all elements from multimap

empty(): This is a Boolean operation. Returns true if multimap is empty. Returns false if multimap is not empty.

Example program to explain all above functions is:

#include <iostream>
#include <map>
#include <iterator>

using namespace std;

int main()
{
    multimap <int, int> mmp;        // declaring an empty multimap
    multimap <int, int> :: iterator it;
	
	// inserting elements
	for (int i=1; i<=5; i++ ) {
		mmp.insert (pair <int, int> (i, 10*i));
	}
 
    // printing elements in  multimap
	cout << "Elements in multimap (key and value) pair wise" << endl;
	
    for (it = mmp.begin(); it != mmp.end(); it++ ) {
        cout  << it->first <<  '\t' << it->second << endl;
    }
    cout << endl;
 
    // assigning all elements of multimap1 to multimap2
    multimap <int, int> mmp2(mmp.begin(),mmp.end());
 
    //printing elements of multimap2
    cout << "Elements in multimap2 (key and value) pair wise" << endl;
	
    for (it = mmp2.begin(); it != mmp2.end(); it++ ) {
        cout  << it->first <<  '\t' << it->second << endl;
    }
    cout << endl;
 
	cout << "Size of the multimap is " ;
	cout << mmp.size() << endl;
	cout << endl << "Maximum size of the multimap is " ;
	cout << mmp.max_size() << endl;
	
	cout << endl << "Applying clear operation on multimap2..." <<endl;
	mmp2.clear();
	
	cout << endl << "Checking wheter multimap2 is  empty or not using empty() operation" << endl;
	if (mmp2.empty()) {
		cout << "Multimap2 is empty " << endl;
	}
	else {
		cout << "Multimap2 is not empty " << endl;
	}
	
	return 0;
}

Output

Elements in multimap (key and value) pair wise
1 10
2 20
3 30
4 40
5 50

Elements in multimap2 (key and value) pair wise
1 10
2 20
3 30
4 40
5 50

Size of the multimap is 5

Maximum size of the multimap is 461168601842738790

Applying clear operation on multimap2…

Checking wheter multimap2 is empty or not using empty() operation
Multimap2 is empty

Some other operations are:

find(key): This will find all elements with specified key value.

erase(key): This will delete the value with specified key value.

swap(): This operations swaps all key value pairs of multimap1 to multimap2 and same way multimap2 to multimap1.

upper_bound(): This will return the iterator to upper bound.

lower_bound(): This will return the iterator to lower bound.

Example program to show above operations:

#include <iostream>
#include <map>
#include <iterator>

using namespace std;

int main()
{
	multimap <int, int> mmp;
	multimap <int, int> mmp2;
	multimap <int, int> :: iterator it;
	multimap <int, int> :: iterator it1;
	multimap <int, int> :: iterator it2;
	
	for (int i=1; i<=5; i++ ) {
		mmp.insert (pair <int, int> (i, 10*i));
	}
 
	for (int i=1; i<=5; i++ ) {
		mmp2.insert (pair <int, int> (i*3, i*i*i));
	}
	
	cout << "Elements in multimap1 before swapping are" << endl;
	
	for (it = mmp.begin(); it != mmp.end(); it++ ) {
		cout  << it->first <<  '\t' << it->second << endl;
	}
	
	cout << endl;
	cout << "Elements in multimap2 before swapping are" << endl;
	for (it = mmp2.begin(); it != mmp2.end(); it++ ) {
		cout  << it->first <<  '\t' << it->second << endl;
	}
	
	cout << endl;
	cout << "Performing swapping operation......" << endl;
	mmp.swap(mmp2);
	cout << "Elements in multimap1 after swapping are" << endl;
	
	for (it = mmp.begin(); it != mmp.end(); it++ ) {
		cout  << it->first <<  '\t' << it->second << endl;
	}
	
	cout << endl;
	cout << "Elements in multimap2 after swapping are" << endl;
	for (it = mmp2.begin(); it != mmp2.end(); it++ ) {
		cout  << it->first <<  '\t' << it->second << endl;
	}
	
	cout << endl;
	it= mmp.begin();
	mmp.erase (it); // erasing first element of the mmp
	
	cout << "After erasing first element in multimap1" << endl;
	for (it = mmp.begin(); it != mmp.end(); it++ ) {
		cout  << it->first <<  '\t' << it->second << endl;
	}
	
	cout << "\nUsing lower bound and upper bound for printing" << endl;
	it1= mmp2.lower_bound(2);
	it2= mmp2.upper_bound(4);
	for (it = it1; it != it2; it++ ) {
		cout  << it->first <<  '\t' << it->second << endl;
	}
	
	return 0;
}

Output

Elements in multimap1 before swapping are
1 10
2 20
3 30
4 40
5 50

Elements in multimap2 before swapping are
3 1
6 8
9 27
12 64
15 125

Performing swapping operation……
Elements in multimap1 after swapping are
3 1
6 8
9 27
12 64
15 125

Elements in multimap2 after swapping are
1 10
2 20
3 30
4 40
5 50

After erasing first element in multimap1
6 8
9 27
12 64
15 125

Using lower bound and upper bound for printing
2 20
3 30
4 40

Comment below if you have any queries related to above stl multimap or std::multimap tutorial.

Leave a Comment

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