unordered set of tuples in C++ with Examples - GeeksforGeeks

19 Dec, 2021
What is a tuple?
A tuple in C++ is an object which is used to group elements together. In a tuple, elements can be of the same data type or different data types. The elements of tuples are initialized as in the order in which they will be accessed. 
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.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.
What is an unordered set?
An unordered set is an unordered associative container that is similar to a set but in the case of an unordered set, elements are not arranged in any particular order. Like a set, an unordered set also contains unique elements. An unordered set is implemented using a hash table internally and keys are hashed into indices of the hash table. Insertion in an unordered set is always unpredictable or randomized. Most operations in an unordered set take constant time O(1) but in the worst case, the time complexity may go up to O(n). 
Functions associated with the unordered set:  
insert(x):Inserts a new element ‘x’ in the unordered set container.
begin(): Returns an iterator pointing to the first element in the unordered_set container.
end(): Returns an iterator pointing to the hypothetical element next to the end element.
count(): Counts the number of times a particular element is present in an unordered_set container.
erase(): Remove either a single element or a range of elements ranging from start to end(exclusive).
size(): Return the number of elements in the unordered_set container.
swap(): Exchange values of two unordered_set containers.
max_size(): Returns maximum number of elements that an unordered_set container can hold.
empty(): Check if an unordered_set container is empty or not.
An unordered set of tuples can be quite useful whenever an algorithm requires a complex data structure. 
This article focuses on how to create an unordered set of tuples in C++. Note that for simplicity we are taking a tuple of three elements into consideration but a tuple can contain more or fewer elements also.
Unordered set of tuples
An unordered set of tuples is an unordered set in which each of the elements is a tuple. Note that by default an unordered set doesn’t have the functionality of tuples. In simple words, one cannot declare an unordered set of tuples directly in C++. One have to pass a Hash function as an argument to the unordered set. 
Syntax:
unordered_set<tuple<dataType1, dataType2, dataType3>, hashFunction> unorderedSetOfTuples;
Here,
dataType1, dataType2, dataType3 are similar or dissimilar data types
hashFunction:
struct hashFunction{    size_t operator()(const tuple<int , int , int>&x) const   {       return get<0>(x) ^ get<1>(x) ^ get<2>(x);   }};
Example 1: Below is the implementation using an unordered set of tuples.
C++
// C++ program to illustrate the// implementation of unorderedSet of// tuples#include <bits/stdc++.h>using namespace std;  // Hash functionstruct hashFunction{  size_t operator()(const tuple<int ,                     int , int>&x) const  {    return get<0>(x) ^ get<1>(x) ^ get<2>(x);  }};  // Function to print unordered set elementsvoid print(unordered_set<tuple<int, int,           int>, hashFunction > &unorderedSetOfTuples){   // Iterating over unorderedSetOfTuples elements  for (auto currentTuple : unorderedSetOfTuples)  {    // Each element is a tuple itself    tuple<int, int, int> tp = currentTuple;      cout << "[ ";      // Printing tuple elements    cout << get<0>(tp) <<      " , " << get<1>(tp) <<      " , " << get<2>(tp) ;    cout << " ]";      cout << "\n";  }  }  // Driver codeint main(){  // Declaring a unordered set of tuples  unordered_set<tuple<int, int,  int>, hashFunction > unorderedSetOfTuples;    // Initializing tuples of int type  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 unordered set  unorderedSetOfTuples.insert(tuple1);  unorderedSetOfTuples.insert(tuple2);  unorderedSetOfTuples.insert(tuple3);  unorderedSetOfTuples.insert(tuple4);  unorderedSetOfTuples.insert(tuple5);    // Calling print function  print(unorderedSetOfTuples);  return 0;}
Output:
[ 2 , 1 , 4 ][ 4 , 2 , 3 ][ 2 , 3 , 5 ]
Explanation:
In the above output, elements or tuples are not arranged in any particular order.
Example 2: Below is the implementation using an unordered set of tuples.
C++
// C++ program to illustrate the// implementation of unordered set of// tuples#include <bits/stdc++.h>using namespace std;   // Hash functionstruct hashFunction{  size_t operator()(const tuple<bool,                     bool, bool>&x) const  {    return get<0>(x) ^ std::get<1>(x) ^ std::get<2>(x);  }};  // Function to print unorderedSet elementsvoid print(unordered_set<tuple<bool, bool,           bool>, hashFunction > &unorderedSetOfTuples){  // Iterating over unorderedSetOfTuples elements  for (auto currentTuple : unorderedSetOfTuples)  {    // Each element is a tuple itself of     // bool type    tuple<bool, bool, bool> tp = currentTuple;      cout << "[ ";      // Printing tuple elements    cout << get<0>(tp) <<            " , " << get<1>(tp) <<            " , " << get<2>(tp) ;    cout << " ]";      cout << "\n";  }  }  // Driver codeint main(){  // Declaring a unordered set of tuples  unordered_set<tuple<bool, bool,  bool>, hashFunction > unorderedSetOfTuples;    // Initializing tuples of bool type  tuple<bool, bool,        bool> tuple1;  tuple1 = make_tuple(0, 1, 1);    tuple<bool, bool,        bool> tuple2;  tuple2 = make_tuple(1, 1, 1);    tuple<bool, bool,        bool> tuple3;  tuple3 = make_tuple(0, 1, 1);    tuple<bool, bool,        bool> tuple4;  tuple4 = make_tuple(0, 0, 0);    tuple<bool, bool,        bool> tuple5;  tuple5 = make_tuple(1, 1, 0);    tuple<bool, bool,        bool> tuple6;  tuple6 = make_tuple(0, 1, 1);    // Inserting into the unordered set  unorderedSetOfTuples.insert(tuple1);  unorderedSetOfTuples.insert(tuple2);  unorderedSetOfTuples.insert(tuple3);  unorderedSetOfTuples.insert(tuple4);  unorderedSetOfTuples.insert(tuple5);  unorderedSetOfTuples.insert(tuple6);    // Calling print function  print(unorderedSetOfTuples);  return 0;}
Output:
[ 1 , 1 , 0 ][ 0 , 0 , 0 ][ 0 , 1 , 1 ][ 1 , 1 , 1 ]
Explanation:
In the above output also, elements are not arranged in any particular order. This confirms that in an unordered set keys are hashed into the indices of the hash table in a randomized manner. Also, tuple3 and tuple6 have the same set of boolean values, that is why only one copy of the tuple is present in the set. 
cpp-tuple
cpp-unordered_set
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Sorting a vector in C++
Friend class and function in C++
Pair in C++ Standard Template Library (STL)
std::string class in C++
Queue in C++ Standard Template Library (STL)
Inline Functions in C++
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++