Difference between pair in Multiset and Multimap in C++ STL

Pairs in C++: The pair container is a simple container defined in <utility> header consisting of two data elements or objects. The first element is referenced as ‘first’ and the second element as ‘second’ and the order is fixed (first, second). Pair is used to combine together two values which may be different in type. Pair provides a way to store two heterogeneous objects as a single unit.

Syntax:

pair (data_type1, data_type2) Pair_name;

Multiset in C++: Multiset is a type of associative containers that store elements following a specific order, and where multiple elements can have same values.

Syntax:



multiset <data_type> Multiset_name;

Multimap: Multi-map is a type of associative containers which is similar to the map with an exception that multiple elements can have same keys.

Syntax:

multimap <data_type1, data_type2> Multimap_name

What is the difference between pair in multi-set and multi-map in C++ STL?

The default behavior of both of these data structures multiset and multimap is to store elements in ascending order. When a pair of multiset is created then by default, it will sort all the pairs in increasing order according to the first element of all the pairs and if the first element of any two or more than two pairs are equal then it will sort the pair according to the second element of the pair.

When a pair of multimap  is created then by default, it will sort all the pairs in increasing order according to the first element of all the pairs and if the first element of any two or more than two pair are equal then it will print the pair according to the order of insertion to the pair of multimap.

Below are the programs to illustrate the difference:



Program 1: Pair in multi-set 
 

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program print the data of
// multiset by inserting using pair
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the data stored
// in pair of multiset
void printData(multiset<pair<int, string> > gfg)
{
  
    // Declare iterator
    multiset<pair<int, string> >::iterator i;
  
    // Iterate through pair of multiset
    for (i = gfg.begin(); i != gfg.end(); ++i) {
  
        // Print the pairs
        cout << i->first << " " 
<< i->second << endl;
    }
}
  
// Driver Code
int main()
{
    // Declare pair of multiset
    multiset<pair<int, string> > gfg;
  
    // Insert Data
    gfg.insert(make_pair(1, "yukti"));
    gfg.insert(make_pair(2, "umang"));
    gfg.insert(make_pair(3, "vinay"));
    gfg.insert(make_pair(3, "vijay"));
    gfg.insert(make_pair(4, "kanak"));
  
    // Function call to print the data
    printData(gfg);
    return 0;
}
chevron_right

Explanation:
In the above program we have created pairs of integer and string in which names are paired with each integer and are inserted in the multi-set. According to default behavior of multi-set the data is arranged in ascending order according to first element but when first element is same it will arrange those elements according to the second value. For the pair (3, “vijay”) and (3, “vinay”) the first element in the pair i.e., 3 is same for both “vijay” and “vinay” so it will arrange the pairs according to second element “vijay” then “vinay” (in alphabetical sequence).

Program 2: Pair in multi-map
 

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program print the data of
// multimap by inserting using pair
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the data stored
// in pair of multimap
void printData(multimap<int, string> gfg)
{
  
    // Declare iterator
    multimap<int, string>::iterator i;
  
    // Iterate through pair of multiset
    for (i = gfg.begin(); i != gfg.end(); ++i) {
  
        // Print the pairs
        cout << i->first << " " 
<< i->second << endl;
    }
}
  
// Driver Code
int main()
{
    // Declare pair of multimap
    multimap<int, string> gfg;
  
    // Insert data
    gfg.insert(make_pair(1, "yukti"));
    gfg.insert(make_pair(2, "umang"));
    gfg.insert(make_pair(3, "vinay"));
    gfg.insert(make_pair(3, "vijay"));
    gfg.insert(make_pair(4, "kanak"));
  
    // Function call to print the data
    printData(gfg);
  
    return 0;
}
chevron_right

Output
1 yukti
2 umang
3 vinay
3 vijay
4 kanak

Explanation of above Code:
In the above program, we have again inserted the same pairs but this time in multi-map . According to default behavior of multi-map the data is arranged in ascending order according to key but when key is same unlike the multi-set it will see the precedence which element is inserted first and then it will arrange according to that sequence. So, as in the output shown we can see that as the key 3 is same for “vinay” and “vijay” so it will follow the sequence in which the pairs were inserted in the multi-map, that’s why, “vinay” came first before “vijay” in output.

Tabular Differentiation:

Pair in Multiset Multimap
In pair of multiset pair is used to mapped key with specific value. Default behaviour is to insert element as a key-value pair.
When a pair of a multiset is created then by default, it will sort all the pairs in increasing order according to the first element of all the pairs and if the first element of any two or more than two pairs are equal then it will sort the pair according to the second element of the pair. When a pair of a multimap is created then by default, it will sort all the pairs in increasing order according to the first element of all the pairs and if the first element of any two or more than two pair are equal then it will print the pair according to the order of insertion to the pair of multimap.
Syntax:

multiset<pair<int, string> > M;

Syntax:

multiset<int, string> M;

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.





I love to express myself as well as my skills I always gear up to contribute to the organization i work or the society

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :