Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Set of Tuples in C++ with Examples

  • Difficulty Level : Easy
  • Last Updated : 15 Dec, 2021

What is a tuple?
A tuple is an object that can hold a number of elements. The elements can be of different data types. The elements of tuples are initialized as arguments in the order in which they will be accessed.

Operations on tuple:
1. 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.
2. 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.

What is a set?
Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. 

Some basic functions associated with Set: 
1. begin(): Returns an iterator to the first element in the set.
2. end(): Returns an iterator to the theoretical element that follows the last element in the set.
3. size(): Returns the number of elements in the set.
4. max_size(): Returns the maximum number of elements that the set can hold.
5. empty(): Returns whether the set is empty.

A set of tuples can be quite useful while implementing complex data structures. Below is the C++ program to implement the above approach-

C++




// C++ program to demonstrate the 
// implementation of set of
// tuples
#include <bits/stdc++.h>
using namespace std;
  
// Function to print set contents
void print(set<tuple<int, int
                     int> >& setOfTuples)
{
  for (auto x : setOfTuples) 
  {
    tuple<int, int, int> tp = x;
    cout << get<0>(tp) << 
            ' ' << get<1>(tp) << 
            ' ' << get<2>(tp) << '\n';
  }
}
  
// Driver code
int main()
{
  // Declaring a set of tuples
  set<tuple<int, int
            int> > setOfTuples;
  
  // 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, 4);
  
  tuple<int, int
        int> tuple4;
  tuple4 = make_tuple(2, 1, 4);
  
  tuple<int, int
        int> tuple5;
  tuple5 = make_tuple(5, 8, 14);
  
  // Inserting into set
  setOfTuples.insert(tuple1);
  setOfTuples.insert(tuple2);
  setOfTuples.insert(tuple3);
  setOfTuples.insert(tuple4);
  setOfTuples.insert(tuple5);
  
  // Calling print function
  print(setOfTuples);
  
  return 0;
}
Output
1 2 3
2 1 4
2 3 4
2 3 5
5 8 14

By default, tuples are arranged in non – descending order in the set and follows the below logic:
In the set, 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.

Below is the C++ program to implement the above approach-

C++




// C++ program to demonstrate the 
// implementation of set 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 requirement
struct cmp 
{
  bool operator()(const tuple<int, int
                              int>& x,
                  const tuple<int, int
                              int>& y)
  {
  
    if (get<0>(x) == get<0>(y))
    {
      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 set elements
void print(set<tuple<int, int
                     int>, cmp>& setOfTuples)
{
  for (auto x : setOfTuples) 
  {
    tuple<int, int, int> tp = x;
    cout << get<0>(tp) << 
            ' ' << get<1>(tp) << 
            ' ' << get<2>(tp) << '\n';
  }
}
  
// Driver code
int main()
{
  // Declaring a set of tuples
  set<tuple<int, int
            int>, cmp> setOfTuples;
  
  // 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, 4);
  
  tuple<int, int
        int> tuple4;
  tuple4 = make_tuple(2, 1, 4);
  
  tuple<int, int
        int> tuple5;
  tuple5 = make_tuple(5, 8, 14);
  
  // Inserting into set
  setOfTuples.insert(tuple1);
  setOfTuples.insert(tuple2);
  setOfTuples.insert(tuple3);
  setOfTuples.insert(tuple4);
  setOfTuples.insert(tuple5);
  
  // Calling print function
  print(setOfTuples);
  
  return 0;
}
Output
5 8 14
2 3 5
2 3 4
2 1 4
1 2 3


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!