Open In App

Multiset of Pairs in C++ with Examples

What is Multiset?

A multiset is an associative container that can hold a number of elements in a specific order. Unlike a set, a multiset can contain multiple occurrences of the same element.



Some of the functions associated with a multiset:  

What is Pair?



Utility header in C++ provides us pair container. A pair consists of two data elements or objects.

How to access a pair?

To access elements of a pair use the dot (.) operator.

Syntax:

auto fistElement = myPair.first;
auto fistElement = myPair.second;

Multiset of pairs

A multiset of pairs is a multiset in which each element is a pair itself. Two pairs are considered to be equal if the corresponding first and second elements of pairs are equal. Now if there is a need to store more than one copy of a pair along with other elements that too in a particular order, in such cases multiset of pairs comes in handy. 

Syntax:

multiset<pair<dataType1, dataType2>> myMultiset;

 Here,

dataType1 and dataType2 can be similar or dissimilar data types.

Example 1: Below is the C++ program to demonstrate the working of a multiset of pairs having integer values.




// C++ program to illustrate the
// implementation of multiset of
// pairs
#include <bits/stdc++.h>
using namespace std;
 
// Function to print multiset
// elements
void print(multiset<pair<int,
           int>> &multisetOfPairs)
  // Iterating over multiset of
  // pairs elements
  for (auto cuurentPair : multisetOfPairs)
  {
    // Each element is a tuple itself
    pair<int, int> pr = cuurentPair;
 
    // Printing pair elements
    cout << "[ " << pr.first << ' ' <<
             pr.second << " ]"<< '\n';
  }
}
 
// Driver code
int main()
{
  // Declaring a multiset of tuples
  multiset<pair<int,
  int>> multisetOfPairs;
 
  // Initializing a pair
  pair<int, int> pair1;
  pair1 = make_pair(1, 2);
 
  // Initializing a pair
  pair<int, int> pair2;
  pair2 = make_pair(3, 4);
 
  // Initializing another pair
  pair<int, int> pair3;
  pair3 = make_pair(5, 6);
 
  // Initializing another pair
  pair<int, int> pair4;
  pair4 = make_pair(7, 8);
 
  // Initializing another pair
  pair<int, int> pair5;
  pair5 = make_pair(9, 10);
 
  // Inserting into multiset
  multisetOfPairs.insert(pair1);
  multisetOfPairs.insert(pair2);
  multisetOfPairs.insert(pair3);
  multisetOfPairs.insert(pair4);
  multisetOfPairs.insert(pair5);
 
  // Calling print function
  print(multisetOfPairs);
 
  return 0;
}

Output:

[ 1 2 ]
[ 3 4 ]
[ 5 6 ]
[ 7 8 ]
[ 9 10 ]

Time complexity: O(n* log n). //n is the size of the multiset.

Space complexity: O(n).

Explanation:

In the above output, the elements are arranged in sorted order of pairs in the multiset of pairs.

Example 2: Below is the C++ program to demonstrate the working of a multiset of pairs having string values.




// C++ program to illustrate the
// implementation of multiset of
// pairs
#include <bits/stdc++.h>
using namespace std;
 
// Function to print multiset elements
void print(multiset<pair<string,
           string>> &multisetOfPairs)
{
  // Iterating over multiset of pairs elements
  for (auto currentPair : multisetOfPairs)
  {
    // Each element is a pair itself
    pair<string, string> pr = currentPair;
 
    // Printing pair elements
    cout << "[ " << pr.first << ' ' <<
             pr.second << " ]"<< '\n';
  }
}
 
// Driver code
int main()
{
  // Declaring a multiset of pairs
  multiset<pair<string,
  string>> multisetOfPairs;
 
  // Initializing a pair
  pair<string, string> pair1;
  pair1 = make_pair("GeeksforGeeks",
                    "GFG");
 
  // Initializing a pair
  pair<string, string> pair2;
  pair2 = make_pair("Swift",
                    "Python");
 
  // Initializing another pair
  pair<string, string> pair3;
  pair3 = make_pair("C++", "C");
 
  // Initializing another pair
  pair<string, string> pair4;
  pair4 = make_pair("PHP",
                    "HTML");
 
  // Initializing another pair
  pair<string, string> pair5;
  pair5 = make_pair("Javascript",
                    "CSS");
 
  // Inserting into multiset
  multisetOfPairs.insert(pair1);
  multisetOfPairs.insert(pair2);
  multisetOfPairs.insert(pair3);
  multisetOfPairs.insert(pair4);
  multisetOfPairs.insert(pair5);
 
  // Calling print function
  print(multisetOfPairs);
 
  return 0;
}

Output:

[ C++ C ]
[ GeeksforGeeks GFG ]
[ Javascript CSS ]
[ PHP HTML ]
[ Swift Python ]

Time complexity: O(n* log n). //n is the size of the multiset.

Space complexity: O(n).

Explanation:

In the above output, the elements are arranged in sorted order of pairs in the multiset of pairs.


Article Tags :
C++