Set of Tuples in C++ with Examples
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-
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-
5 8 14 2 3 5 2 3 4 2 1 4 1 2 3