Multimap in STL C++

About Multimap in STL :

Multimap in STL is defined as the container used to store the key-value pair in which it is not necessary the key will always be unique.

Multimap in stl

Working of Multimap :

In STL, The difference between a map and a multimap is that multiple components can share the same keys. Additionally, it is not necessary in this scenario for the key-value and mapped value pair to be unique.Multimap always maintains the keys arranged, which is a crucial point to keep in mind.Multimap is incredibly valuable in competitive programming due of these characteristics. 

Functions in Multimap :

 Function Working
insert({}) insert a pair of key – value to the map
count() return the number of matches element from the map.
size() give the number of keys present in the map.
empty() tells whether the map is empty or not.
erase(const it) remove the element at the position pointed by iterator.

Example of Multimap in STL :

Run
#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
    multimap< int,int> mpp;
    
    mpp.insert({1,10});
    mpp.insert({2,20});
    mpp.insert({4,40});
    mpp.insert({8,80});
    mpp.insert({6,60});
    mpp.insert({3,30});
    mpp.insert({1,20});
    mpp.insert({4,50});
    
    cout<<"The key and value in the map : "<< endl;
    for(auto it : mpp){
        cout<< it.first<<" "<< it.second<< endl;
    }
    cout<< endl;
    cout<<"The size of map :"<< mpp.size();
    return 0;
}

Output :

The key and value in the map : 
1 10
1 20
2 20
3 30
4 40
4 50
6 60
8 80

The size of map :8

In the above program, we take the multimap mpp, with elements and print them on the screen. Here, we can see that multiple key of same value can be present.

Example of Multimap in STL:

Run
#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
    multimap mpp;
    
    mpp.insert({1,10});
    mpp.insert({2,20});
    mpp.insert({4,40});
    mpp.insert({8,80});
    mpp.insert({6,60});
    mpp.insert({3,30});
    mpp.insert({1,20});
    mpp.insert({4,50});
    mpp.insert({5,60});
    mpp.insert({6,70});
    cout<<"The key and value in the map : "<< endl;
    for(auto it : mpp){
        cout<< it.first<<" "<< it.second<< endl;
    }
    cout<< endl;
    cout<< "The size of map :"<< mpp.size()<< endl;
    if(mpp.empty()){
        cout<<"Multimap is empty"<< endl;
    }
    else{
        cout<<"Multimap is not empty";
    }
    return 0;
}

Output :

The key and value in the map : 
1 10
1 20
2 20
3 30
4 40
4 50
5 60
6 60
6 70
8 80

The size of map :10
Multimap is not empty

In the above program, we take the multimap mpp, with elements and print them on the screen. Here, we can see that multiple key of same value can be present. and check whether the multimap is empty

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription