Multiset of Pairs in C++ with Examples
Last Updated :
20 Feb, 2023
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:
- begin(): Returns an iterator to the first element in the multiset.
- end(): Returns an iterator to the theoretical element that follows the last element in the multiset.
- size(): Returns the number of elements in the multiset.
- max_size(): Returns the maximum number of elements that the multiset can hold.
- empty(): Returns whether the multiset is empty.
What is Pair?
Utility header in C++ provides us pair container. A pair consists 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 that may be different in type. Pair provides a way to store two heterogeneous objects as a single unit.
- Pair can be assigned, copied, and compared. The array of objects allocated in a map or hash_map is of type ‘pair’ by default in which all the ‘first’ elements are unique keys associated with their ‘second’ value objects.
- To access the elements, we use variable name followed by dot operator followed by the keyword first or second.
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++
#include <bits/stdc++.h>
using namespace std;
void print(multiset<pair< int ,
int >> &multisetOfPairs)
{
for ( auto cuurentPair : multisetOfPairs)
{
pair< int , int > pr = cuurentPair;
cout << "[ " << pr.first << ' ' <<
pr.second << " ]" << '\n' ;
}
}
int main()
{
multiset<pair< int ,
int >> multisetOfPairs;
pair< int , int > pair1;
pair1 = make_pair(1, 2);
pair< int , int > pair2;
pair2 = make_pair(3, 4);
pair< int , int > pair3;
pair3 = make_pair(5, 6);
pair< int , int > pair4;
pair4 = make_pair(7, 8);
pair< int , int > pair5;
pair5 = make_pair(9, 10);
multisetOfPairs.insert(pair1);
multisetOfPairs.insert(pair2);
multisetOfPairs.insert(pair3);
multisetOfPairs.insert(pair4);
multisetOfPairs.insert(pair5);
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++
#include <bits/stdc++.h>
using namespace std;
void print(multiset<pair<string,
string>> &multisetOfPairs)
{
for ( auto currentPair : multisetOfPairs)
{
pair<string, string> pr = currentPair;
cout << "[ " << pr.first << ' ' <<
pr.second << " ]" << '\n' ;
}
}
int main()
{
multiset<pair<string,
string>> multisetOfPairs;
pair<string, string> pair1;
pair1 = make_pair( "GeeksforGeeks" ,
"GFG" );
pair<string, string> pair2;
pair2 = make_pair( "Swift" ,
"Python" );
pair<string, string> pair3;
pair3 = make_pair( "C++" , "C" );
pair<string, string> pair4;
pair4 = make_pair( "PHP" ,
"HTML" );
pair<string, string> pair5;
pair5 = make_pair( "Javascript" ,
"CSS" );
multisetOfPairs.insert(pair1);
multisetOfPairs.insert(pair2);
multisetOfPairs.insert(pair3);
multisetOfPairs.insert(pair4);
multisetOfPairs.insert(pair5);
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.
Share your thoughts in the comments
Please Login to comment...