Multiset of Tuples in C++ with Examples

24 Jan, 2022
What is a tuple?
A tuple in C++ is an object which binds a group of elements together. The elements can be similar as well as different data types. The elements of tuples are initialized as in the order in which they will be accessed.
Syntax:
tuple<data_type1, data_type2, dataType3, ....> myTuple 
Here,dataType1, dataType2, dataType3 . . . . are similar or dissimilar data types
Functions associated with a tuple:
1. make_tuple(): make_tuple() is used to assign tuple with values. The values passed should be in order with the values declared in the tuple.
Syntax:
myTuple = make_tuple(value1, value2, value3,....)
2. get(): get() is used to access the tuple values and modify them, it accepts the index and tuple name as arguments to access a particular tuple element.
Syntax:
get<index>(myTuple)
Here,index is the index of the element we want to access in myTuple. Indexing in a tuple begins with zero. 
What is a multiset?
A multiset is a type of associative container that is similar to a set but in the case of a multiset, all elements are necessary to be pairwise distinct. In simple words, there can be more than one element with the same value.
Syntax:
multiset<dataType> myMultiset;
Here,dataType is a data type.
Functions associated with 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.
A multiset of tuples can be quite useful when an algorithm requires a complex data structure. This article focuses on how to create a multiset of tuples in C++. Note that for simplicity a tuple of three elements is taken into consideration but a tuple can contain more or fewer elements also.
Multiset of tuples
A multiset of tuples is a multiset in which each of the elements is a tuple. 
Syntax:
multiset<tuple<dataType1, dataType2, dataType3>> multisetOfTuples;
Here,dataType1, dataType2, dataType3 are similar or dissimilar data types.
Example 1: Below is the C++ program to implement a multiset of tuples:
C++
// C++ program to illustrate the// implementation of multiset of// tuples#include <bits/stdc++.h>using namespace std;  // Function to print multiset contentsvoid print(multiset<tuple<int, int,           int> >& multisetOfTuples) {  // Iterating over multisetOfTuples elements  for (auto x : multisetOfTuples)  {    // Each element is a tuple itself    tuple<int, int, int> tp = x;     // Printing tuple elements    cout << get<0>(tp) <<            ' ' << get<1>(tp) <<            ' ' << get<2>(tp) << '\n';  }} // Driver codeint main(){  // Declaring a multiset of tuples  multiset<tuple<int, int,  int> > multisetOfTuples;   // Initializing tuples  tuple<int, int,  int> tuple1;  tuple1 = make_tuple(4, 2, 3);   tuple<int, int,  int> tuple2;  tuple2 = make_tuple(2, 3, 5);   tuple<int, int,  int> tuple3;  tuple3 = make_tuple(2, 3, 5);   tuple<int, int,  int> tuple4;  tuple4 = make_tuple(2, 1, 4);   tuple<int, int,  int> tuple5;  tuple5 = make_tuple(4, 2, 3);    // Inserting into multiset  multisetOfTuples.insert(tuple1);  multisetOfTuples.insert(tuple2);  multisetOfTuples.insert(tuple3);  multisetOfTuples.insert(tuple4);  multisetOfTuples.insert(tuple5);   // Calling print function  print(multisetOfTuples);   return 0;}
Output:
2 1 42 3 52 3 54 2 34 2 3
Explanation:
In the above output, there is a total of five tuples are present in the multiset out of which (2, 3, 5) and (4, 2, 3) occur twice. By default, tuples are arranged in non-descending order in the multiset and follow the below logic:
In the multiset, if the first value of two tuples is equal then the second value of tuples is compared and if it is also equal then the third value is compared.But it is always possible to pass a comparator to a set.
In the multiset, if the first value of two tuples is equal then the second value of tuples is compared and if it is also equal then the third value is compared.
But it is always possible to pass a comparator to a set.
Example 2: Below is the C++ program to demonstrate passing a comparator to the multiset which arranges a multiset of tuples elements in non-ascending order.
C++
// C++ program to demonstrate the// implementation of multiset of// tuples by using custom comparator#include <bits/stdc++.h>using namespace std;  // Comparator for arranging elements// in non-ascending order We can// always modify the comparator as// per the requirementstruct cmp{  // Arranging multiset elements in  // non-ascending order  bool operator()(const tuple<int, int,                              int>& x,                  const tuple<int, int,                              int>& y)  {    // If first elements of corresponding    // tuples are equal    if (get<0>(x) == get<0>(y))    {      // If second elements of corresponding      // tuples are equal      if (get<1>(x) == get<1>(y))        return get<2>(x) > get<2>(y);             return get<1>(x) > get<1>(y);    }     return get<0>(x) > get<0>(y);  }}; // Function to print multiset elementsvoid print(multiset<tuple<int, int,           int>, cmp>& multisetOfTuples){  for (auto x : multisetOfTuples)  {    // Each element of the multiset is    // tuple itself    tuple<int, int, int> tp = x;     // Printing tuple elements    cout << get<0>(tp) <<            ' ' << get<1>(tp) <<            ' ' << get<2>(tp) << '\n';  }} // Driver codeint main(){  // Declaring a multiset of tuples  multiset<tuple<int, int,                 int>, cmp> multisetOfTuples;   // Initializing tuples  tuple<int, int,        int> tuple1;  tuple1 = make_tuple(1, 2, 3);   tuple<int, int,        int> tuple2;  tuple2 = make_tuple(2, 3, 5);   tuple<int, int,        int> tuple3;  tuple3 = make_tuple(2, 3, 5);   tuple<int, int,        int> tuple4;  tuple4 = make_tuple(2, 1, 4);   tuple<int, int,        int> tuple5;  tuple5 = make_tuple(5, 8, 4);   // Inserting into multiset  multisetOfTuples.insert(tuple1);  multisetOfTuples.insert(tuple2);  multisetOfTuples.insert(tuple3);  multisetOfTuples.insert(tuple4);  multisetOfTuples.insert(tuple5);   // Calling print function  print(multisetOfTuples);   return 0;}
Output:
5 8 42 3 52 3 52 1 41 2 3
Explanation:
In the above output, elements in the multiset are arranged according to non-ascending order. The tuple with values (2, 3, 5) has two copies in the multiset.
surindertarika1234
bhuwanesh
varshagumber28
cpp-multiset
cpp-tuple
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Bitwise Operators in C/C++
Priority Queue in C++ Standard Template Library (STL)
Set in C++ Standard Template Library (STL)
vector erase() and clear() in C++
unordered_map in C++ STL
Substring in C++
Object Oriented Programming in C++
The C++ Standard Template Library (STL)
Inheritance in C++
C++ Classes and Objects