What is a multimap in C++ STL?

Multimaps are associative containers available in the C++ Standard Library. They are similar to a map with an addition that multiple elements can have the same keys. The key values are used to sort and uniquely identify the elements, while the mapped values store the content associated to this key. They are typically implemented as binary search trees.

There are 3 ways to initialize a multimap

1. Empty constructor
This creates an empty multimap

multimap< data_type, data_type > map_name;

2. Range constructor
It constructs a multimap from another container from the range [first,last), in the same order.
Note: The other container must have pair values;

multimap< data_type, data_type > map_name( iterator_begin, iterator_end );

3. Copy constructor
Constructs a container with a copy of each of the elements in the container specified

multimap< data_type, data_type > map_name( map_2 );

Here are various iterators and functions associated with multimaps in the C++ Standard Library

Iterators
They are used to traverse a multimap and are similar to array pointers.

  1. multimap::begin() – Returns an iterator pointing to the first element in the multimap.
  2. multimap::end() – Returns an iterator pointing to an imaginary element after the last element in the multimap.
  3. multimap::rbegin() – Returns a reverse iterator pointing to the last element in the multimap.
  4. multimap::rend() – Returns a reverse iterator pointing to an imaginary element before the first element.

Example

#include<iostream>
#include<map>

using namespace std;


int main()
{
    multimap<char,int> m;

    m.insert( pair<int,int> ('a',1) );
    m.insert( pair<int,int> ('b',2) );
    m.insert( pair<int,int> ('c',3) );
    m.insert( pair<int,int> ('d',4) );

    multimap<char, int>::iterator it1;
    multimap<char, int>::reverse_iterator it2;

    cout<<"Elements in standard order:\n";
    for( it1 = m.begin(); it1 != m.end(); ++it1)
    {
        cout<< it1->first << " : "<< it1->second<<endl;
    }
    cout<<endl;

    cout<<"Elements in reverse order:\n";
    for( it2 = m.rbegin(); it2 != m.rend(); ++it2)
    {
        cout<< it2->first << " : "<< it2->second<<endl;
    }

}

Output:

Elements in standard order:
a : 1
b : 2
c : 3
d : 4

Elements in reverse order:
d : 4
c : 3
b : 2
a : 1

There are various methods pertaining to multimaps in the C++ STL

  1. multimap::insert(): Inserts an element in the multimap
  2. multimap::clear(): Makes the multimap empty
  3. multimap::size(): Returns the number of elements in the multimap
  4. multimap::count(): Returns number of elements that have the same key value as specified
  5. multimap::find(): Returns an iterator to the first occurrence of an element with the specified key
  6. multimap::lower_bound(): Returns an iterator to the first element whose key is not considered to go before the specified key
  7. multimap::upper_bound(): Returns an iterator to the first element whose key is considered to go after the specified key

Example

#include<iostream>
#include<map>

using namespace std;


int main()
{
    multimap<char,int> m;

    m.insert( pair<int,int> ('a',1) ); //inserting element
    m.insert( pair<int,int> ('b',2) );
    m.insert( pair<int,int> ('b',4) );
    m.insert( pair<int,int> ('b',6) );
    m.insert( pair<int,int> ('c',3) );

    cout<<"Size: "<<m.size()<<endl; //size

    multimap<char, int>::iterator it;

    cout<<"Number of elements with key 'b': "<<m.count('b')<<endl; //count

    cout<<"Value of element with key 'c': "<<m.find('c')->second<<endl; //find



    cout<<"lower bound of 'b':\n";
    cout<<m.lower_bound('b')->first<<" : "<<m.lower_bound('b')->second<<endl; //lower bound

    cout<<"upper bound of 'b':\n";
    cout<<m.upper_bound('b')->first<<" : "<<m.upper_bound('b')->second; //upper bound

}

Output:

Size: 5

Number of elements with key 'b': 3

Value of element with key 'c': 3

lower bound of 'b':
b : 2

upper bound of 'b':
c : 3

This article is written by

Please comment below, if you have any doubts or find any error in the above article.

Leave a Comment

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

Scroll to Top