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:
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 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.
Program:
CPP
// C++ program to create Set of Pairs
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pairs;
// Constant reference
// reference for speed const to avoid changing values
void display(const set<pairs>& s)
{
bool found = false;
// range-based for loop
for (auto const &x : s) {
found = true;
cout << "(" << x.first << ", "
<< x.second << ")"
<< " ";
}
if (not found) {
cout << "No valid pair\n";
}
}
int main()
{
vector<int> v{ 2, 3, 1, 6, 8, 8, 10, 2 };
set<pairs> s;
for (int i = 0; i < v.size() - 1; i++) {
for (int j = i + 1; j < v.size(); j++) {
// check for even number
if (v[i] % 2 == 0 && v[j] % 2 == 0) {
// makes pairs of even numbers
pairs x = make_pair(v[i], v[j]);
// inserts into the set
s.insert(x);
}
}
}
// to display the pairs
display(s);
// to clear the set
s.clear();
}
Output:
(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)
Similar Reads
How to Create a Set of Pairs in C++? In C++, sets are associative containers that store unique elements. On the other hand, pairs allow the users to store two data of different or the same type into a single object. In this article, we will learn how we can create a set of pairs in C++. Example Input: p1={1, 2} p2 ={3, 4} Output: Eleme
2 min read
Vector of sets in C++ Prerequisite: Vectors in C++ STL Vectors are known as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container automatically. Sets are a type of associative containers in which each elem
7 min read
How to Iterate Over a Set of Pairs in C++? In C++, a set is a container that stores unique values in some specified order while pairs are data structures that store key-value pairs. In this article, we will look at how we can iterate through a set of pairs in C++. For Example, Input: mySet = { {100: "Geek"}, {200:" for"}, {300:" Geeks"} } Ou
2 min read
How to Create a Stack of Pairs in C++? In C++, Stacks are a type of container adaptor with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. A pair is a simple container that stores data in a key and value format. In this article, we will learn how to crea
2 min read
Array of Sets in C++ STL An array is a collection of items stored at contiguous memory locations. It is to store multiple items of the same type together. This makes it easier to get access to the elements stored in it by the position of each element. Sets are a type of associative container in which each element has to be
5 min read