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.
unordered_set<tuple<dataType1, dataType2, dataType3>, hashFunction> unorderedSetOfTuples;
dataType1, dataType2, dataType3 are similar or dissimilar data types
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.
[ 2 , 1 , 4 ]
[ 4 , 2 , 3 ]
[ 2 , 3 , 5 ]
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.
[ 1 , 1 , 0 ]
[ 0 , 0 , 0 ]
[ 0 , 1 , 1 ]
[ 1 , 1 , 1 ]
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.