Pairs in Multiset & Multimap in C++

DIFFERENCE BETWEEN PAIRS IN MULTISET & MULTIMAP IN C++
DIFFERENCE BETWEEN PAIRS IN MULTISET & MULTIMAP IN C++

The pair is a simple container defined in <utility> class having two elements or objects. The first element is always referenced as “first” and the second element is always referenced as “second”. The order is fixed i.e, (first, second), it is used to combine two heterogeneous values.

Syntax: pair<data_type 1 , data_type 2> variable name;

IMPLEMENTATION:

#include<iostream>

#include<utility>

using namespace std;
int main()
{
// declaring a pair of int and char
pair PAIR1 ;
PAIR1.first = 100; // 1st element of pair
PAIR1.second = ‘G’ ; // 2nd element of pair
cout << PAIR1.first << ” ” ; // 100
cout << PAIR1.second << endl ; //G
return 0;
}

Multiset in C++

A multiset is a type of an associative container that can have the same number of multiple values. We can store multiple same values.

SYNTAX:
multiset variable_name;

IMPLEMENTATION:
// CPP program to illustrate the
// erase() and count() function
// in multiset

#include<iostream>

#include<algorithm>

#include<set>

using namespace std;
int main()
{
// Create a multiset
multiset s = { 15, 10, 15, 11, 10, 18, 18, 20, 20 };
// PRINT THE MULTISET
cout << “The multiset elements are: “;
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << ” “;
return 0;
}

Multimap in C++

The multimap container in C++ is similar to the map container with an addition that a multimap can have multiple key-value pairs with the same key. Rather than each element is unique, the key-value and mapped value pair have to be unique in this case. Multimap is also implemented using Red-Black trees and hence the basic operations like search, insert, delete works in O(log N) time for multimap as well.

SYNTAX:
multimap variable_name;

IMPLEMENTATION:

#include<iostream>

#include<algorithm>

#include<map>

using namespace std;
int main()
{
// CREATE A MULTIMAP
multimap mp;
//INSERT INTO MULTIMAP
mp.insert({10,20});
mp.insert({5, 50});
mp.insert({10,25});
// PRINT THE MULTIMAP
for(auto x:mp)
cout<<x.first<<” “<<x.second<<endl;
return 0;
}

Difference between pair in multiset and multimap

The default behaviour of both of these data structures multiset and multimap is to store elements in ascending order. 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 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

// 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 > g)
{
// Declare iterator
multiset >::iterator i;
// Iterate through pair of multiset
for (i = g.begin(); i != g.end(); ++i) {
// Print the pairs
cout << i->first << ” ” << i->second << endl; } } int main() { // Declare pair of multiset multiset > g;
// Insert Data
g.insert(make_pair(1, “yukti”));
g.insert(make_pair(2, “umang”));
g.insert(make_pair(3, “vinay”));
g.insert(make_pair(3, “vijay”));
g.insert(make_pair(4, “kanak”));
// Function call to print the data
printData(g);
return 0;
}

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 the default behaviour of multi-set the data is arranged in ascending order according to the first element but when the first element is the 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

// 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 g)
{
// Declare iterator
multimap::iterator i;
// Iterate through pair of multiset
for (i = g.begin(); i != g.end(); ++i) {
// Print the pairs
cout << i->first << ” ” << i->second << endl;
}
}

int main()
{
// Declare pair of multimap
multimap g;
// Insert data
g.insert(make_pair(1, “yukti”));
g.insert(make_pair(2, “umang”));
g.insert(make_pair(3, “vinay”));
g.insert(make_pair(3, “vijay”));
g.insert(make_pair(4, “kanak”));
// Function call to print the data
printData(g);
return 0;
}

Output
1 yukti
2 umang
3 vinay
3 vijay
4 kanak

Explanation of the above Code: In the above program, we have again inserted the same pairs but this time in multi-map. According to the default behaviour of multi-map the data is arranged in ascending order according to key but when the 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 difference

             PAIR IN MULTISET                      PAIR IN 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;  

To read more articles on C++, click here.

By Mansi Agarwal