C++ STL Multimap Container – std::multimap

In this instructional exercise, you will find out about stl multimap i.e., std:: multimap and all capacities material on it with some model code.

In past articles, we previously found out about std:: map holder. It is an acquainted compartment which offers need to key qualities.

However, the issue with the map is it won’t permit copy esteems. Be that as it may, multimap permits copy esteems. Here in multimap, both key and worth pair get one of a kind.

What’s more, we definitely realize that one of the incredible property of guide and multimap is consistently components naturally embedded into arranged request on the fly.

Keys can’t be changed once embedded. Just conceivable way is we need to erase and refresh with another worth.

Execution of multimaps pursues a twofold search tree sort of usage.

C++ STL Multimap Container – std::multimap

To work with multimap we have to incorporate the guide header record.

#include <map>

Iterators that can be pertinent on multimap:

start(): returns iterator to the start.

end(): returns an iterator as far as possible of the guide.

rbegin(): returns turn around iterator to switch start.

tear(): returns switch iterator to turn around the end.

cbegin(): Returns consistent iterator to the start.

cend(): Returns steady iterator as far as possible.

Pronouncing a multimap:

multimap < datatype, datatype > multiMapName;

This is for key and worth pair.

Presently observe some fundamental tasks on multimap:

Supplement activity: Insert tasks impacts size of multimap.

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

Another kind of inclusion is, we can straightforwardly appoint one multimap components to other multimap components legitimately by giving range utilizing iterators.

multimap new map (oldMmap.begin(), oldMmap.end())

size(): This will restore the number of key esteem matches in the multimap.

max_size(): This will restore what is the most extreme limit of the multimap

clear(): This will truncate all components from multimap

void(): This is a Boolean activity. Returns genuine if multimap is vacant. Returns bogus if multimap isn’t vacant.

Model program to clarify every single above work 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 different tasks are:

find(key): This will discover all components with indicated key worth.

erase(key): This will erase the incentive with indicated key worth.

swap(): This activity swaps all key worth sets of multimap1 to multimap2 and same path multimap2 to multimap1.

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

lower_bound(): This will restore the iterator to bring downbound.

Model program to appear above activities:

#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

Remark beneath on the off chance that you have any questions identified with above stl multimap or std:: multimap instructional exercise.

Leave a Comment

error: Alert: Content is protected!!