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:

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:

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 *