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.
- The pairs in a set are stored in sorted order, sorted by the key i.e. the first value of the pair.
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:
Creating Sets of Pairs
Example: Making pairs of all even numbers present in an array.
Input: 2 3 1 6 5 8 10 9 Output: (2, 6) (2, 8) (2, 10) (6, 8) (6, 10) (8, 10) Input: 4 4 6 4 Output: (4, 4) (4, 6) Input: 24 24 24 24 Output: (24, 24) Input: 7, 100, 53, 81 Output: No valid pair
Below program illustrate the solution to above problem:
In this example, range-base for loop is used with const reference for speed and security which iterates over all elements in the container. Variable ‘x’ is of type ‘pairs’. To access the elements of the pair, use variable name followed by dot operator followed by the keyword ‘first’ or ‘second’, these are public members of class pair.
(2, 2) (2, 6) (2, 8) (2, 10) (6, 2) (6, 6) (6, 8) (6, 10) (8, 2) (8, 8) (8, 10) (10, 2) (10, 10)
- Pairs of complete strings in two sets of strings
- Unordered Sets in C++ Standard Template Library
- Mathematics | Independent Sets, Covering and Matching
- Find the Jaccard Index and Jaccard Distance between the two given sets
- Ways to form n/2 pairs such that difference of pairs is minimum
- Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)
- Sorting Vector of Pairs in C++ | Set 2 (Sort in descending order by first and second)
- Count pairs whose products exist in array
- Convert an array to reduced form | Set 2 (Using vector of pairs)
- Find all pairs (a,b) and (c,d) in array which satisfy ab = cd
- Count pairs from two sorted arrays whose sum is equal to a given value x
- Map of pairs in STL
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Count pairs in an array which have at least one digit common
- Binary search in sorted vector of pairs
- Priority queue of pairs in C++ (Ordered by first)
- Johnson’s algorithm for All-pairs shortest paths | Implementation
- Number of special pairs possible from the given two numbers
- Count pairs from two linked lists whose product is equal to a given value
- Find pairs with given product in a sorted Doubly Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.