GeeksforGeeks App
Open App Browser
Continue

# 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-

## C++

 `// C++ program to demonstrate the ``// implementation of set of``// tuples``#include ``using` `namespace` `std;`` ` `// Function to print set contents``void` `print(set >& 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 > 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 ``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, 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, 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
{"ft_lang_label":"__label__en","ft_lang_prob":0.58517474,"math_prob":0.9760978,"size":4284,"snap":"2023-14-2023-23","text_gpt3_token_len":1323,"char_repetition_ratio":0.21238318,"word_repetition_ratio":0.42213643,"special_character_ratio":0.33053222,"punctuation_ratio":0.18151072,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9965967,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-10T22:37:46Z\",\"WARC-Record-ID\":\"<urn:uuid:550a3ecb-83a4-44e1-9425-9b9b0460143f>\",\"Content-Length\":\"240734\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:bea5de3c-f574-4e97-9000-633289a05500>\",\"WARC-Concurrent-To\":\"<urn:uuid:5e85186f-8b1b-4b8b-b8f1-4eadc3dad7ca>\",\"WARC-IP-Address\":\"104.97.85.57\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/set-of-tuples-in-c-with-examples/\",\"WARC-Payload-Digest\":\"sha1:L3FPOFFY7RJZQDMG7NSZA7XKZRSC34RB\",\"WARC-Block-Digest\":\"sha1:FBYI4LBTH7WWZDZQ2EK2ROFIJIYJLSY4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224646350.59_warc_CC-MAIN-20230610200654-20230610230654-00463.warc.gz\"}"}