# Sets of pairs in C++

Sets are a type of associative containers 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.

Pair is a simple container defined in **<utility>** header consisting of two data elements or objects.

- The first element is referenced as ‘first’ and the second element as ‘second’ and the order is fixed (first, second).
- Pair is used to combine together two values which may be different in type. Pair provides a way to store two heterogeneous objects as a single unit.
- Pair can be assigned, copied and compared. The array of objects allocated in a map or hash_map are of type ‘pair’ by default in which all the ‘first’ elements are unique keys associated with their ‘second’ value objects.
- To access the elements, we use variable name followed by dot operator followed by the keyword first or second.

**Sets of Pairs** help in performing the following operations:

- Add a pair, but do not allow duplicates.
- Remove pairs.
- Get count of distinct pairs.
- Check whether a pair is present in a set or not.

The **syntax** for creating sets of pairs is:

set<pair<datatype1, datatype2>>set_name;

### Creating Sets of Pairs

**Example:** Making pairs of all even numbers present in an array.

Input:2 3 1 6 5 8 10 9Output:(2, 6) (2, 8) (2, 10) (6, 8) (6, 10) (8, 10)Input:4 4 6 4Output:(4, 4) (4, 6)Input:24 24 24 24Output:(24, 24)Input:7, 100, 53, 81Output:No valid pair

Below program illustrate the solution to above problem:

In this example, the iterator **‘itr’** returns a pointer of type ‘pairs’. Store the value pointed by the iterator in a variable ‘x’ of type ‘pairs’. To access the elements of the pair, use variable name followed by dot operator followed by the keyword ‘first’ or ‘second’.

**Program:**

`// C++ program to create Set of Pairs ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `typedef` `pair<` `int` `, ` `int` `> pairs; ` `set<pairs> s; ` ` ` `void` `display() ` `{ ` ` ` `int` `f = 0; ` ` ` `for` `(` `auto` `itr = s.begin(); itr != s.end(); itr++) { ` ` ` ` ` `f = 1; ` ` ` `pairs x = *itr; ` ` ` `cout << ` `"("` `<< x.first << ` `", "` ` ` `<< x.second << ` `")"` ` ` `<< ` `" "` `; ` ` ` `} ` ` ` ` ` `if` `(f == 0) ` ` ` `cout << ` `"No valid pair\n"` `; ` `} ` `int` `main() ` `{ ` ` ` `int` `t, n, j, i; ` ` ` `n = 6; ` ` ` `int` `a[] = { 2, 3, 1, 6, 5, 8, 10, 9 }; ` ` ` ` ` `sort(a, a + n); ` ` ` ` ` `for` `(i = 0; i < n - 1; i++) { ` ` ` `for` `(j = i + 1; j < n; j++) { ` ` ` ` ` `// check for even number ` ` ` `if` `(a[i] % 2 == 0 && a[j] % 2 == 0) { ` ` ` ` ` `// makes pairs of even numbers ` ` ` `pairs x = make_pair(a[i], a[j]); ` ` ` ` ` `// inserts into the set ` ` ` `s.insert(x); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// to display the pairs ` ` ` `display(); ` ` ` ` ` `// to clear the set ` ` ` `s.clear(); ` `} ` |

*chevron_right*

*filter_none*

**Output:**

(2, 6) (2, 8) (6, 8)

## Recommended Posts:

- Pairs of complete strings in two sets of strings
- Ways to form n/2 pairs such that difference of pairs is minimum
- Mathematics | Independent Sets, Covering and Matching
- Map of pairs in STL
- Number of pairs whose sum is a power of 2
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j]
- How to create an unordered_map of pairs in C++?
- Priority queue of pairs in C++ (Ordered by first)
- Count pairs with Bitwise-AND as even number
- Count pairs with Bitwise XOR as EVEN number
- Number of special pairs possible from the given two numbers
- Count of pairs violating BST property
- Number of pairs with Pandigital Concatenation
- Find all pairs (a,b) and (c,d) in array which satisfy ab = cd
- Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.