GeeksforGeeks App
Open App
Browser
Continue

# unordered set of tuples in C++ with Examples

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 ``using` `namespace` `std;`` ` `// Hash function``struct` `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 elements``void` `print(unordered_set, 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 code``int` `main()``{``  ``// Declaring a unordered set of tuples``  ``unordered_set, 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 ``using` `namespace` `std;``  ` `// Hash function``struct` `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 elements``void` `print(unordered_set, 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 code``int` `main()``{``  ``// Declaring a unordered set of tuples``  ``unordered_set, 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.

My Personal Notes arrow_drop_up