The pair container 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.
pair (data_type1, data_type2) Pair_name
Initializing a pair
We can also initialize a pair.
pair (data_type1, data_type2) Pair_name (value1, value2) ;
Different ways to initialize pair:
pair g1; //default pair g2(1, 'a'); //initialized, different data type pair g3(1, 10); //initialized, same data type pair g4(g3); //copy of g3
Another way to initialize a pair is by using the make_pair() function.
g2 = make_pair(1, 'a');
Note: If not initialized, the first value of the pair gets automatically initialized.
- make_pair() : This template function allows to create a value pair without writing the types explicitly.
Pair_name = make_pair (value1,value2);
100 G GeeksForGeeks 1.23 GeeksForGeeks is Best 4.56
operators(=, ==, !=, >=, <=) : We can use operators with pairs as well.
- using equal(=) : It assigns new object for a pair object.
pair& operator= (const pair& pr);
- This Assigns pr as the new content for the pair object. The first value is assigned the first value of pr and the second value is assigned the second value of pr .
- Comparison (==) operator with pair : For given two pairs say pair1 and pair2, the comparison operator compares the first value and second value of those two pairs i.e. if pair1.first is equal to pair2.first or not AND if pair1.second is equal to pair2.second or not .
- Not equal (!=) operator with pair : For given two pairs say pair1 and pair2, the != operator compares the first values of those two pairs i.e. if pair1.first is equal to pair2.first or not, if they are equal then it checks the second values of both.
- Logical( >=, <= )operators with pair : For given two pairs say pair1 and pair2, the =, >, can be used with pairs as well. It returns 0 or 1 by only comparing the first value of the pair.
For pairs like p1=(1,20) and p2=(1,10)
p2<p1 should give 0 (as it compares 1st element only & they are equal so its definitely not less), but that isn’t true. Here the pair compares the second element and if it satisfies then returns 1
(this is only the case when the first element gets equal while using a relational operator > or < only, otherwise these operators work as mentioned above)
swap : This function swaps the contents of one pair object with the contents of another pair object. The pairs must be of same type.
For two given pairs say pair1 and pair2 of same type, swap function will swap the pair1.first with pair2.first and pair1.second with pair2.second.
Before swapping: Contents of pair1 = (A, 1) Contents of pair2 = (B, 2) After swapping: Contents of pair1 = (B, 2) Contents of pair2 = (A, 1)
tie() : This function works the same as in tuples. It creates a tuple of lvalue references to its arguments i.e., to unpack the tuple (or here pair) values into separate variables. Just like in tuples, here are also two variants of tie, with and without “ignore”. “ignore” keyword ignores a particular tuple element from getting unpacked.
However, tuples can have multiple arguments but pairs only have two arguments. So, in case of pair of pairs, unpacking needs to be explicitly handled.
tie(int &, int &) = pair1;
1 2 3 2 3 4 a
This is pair g1 with value Geeks. This is pair g3 with value Quiz This pair was initialized as a copy of pair g2 This is pair g2 with value .com The values of this pair were changed after initialization. This is pair g4 with values 5 and 10 made for showing addition. The sum of the values in this pair is 15. We can concatenate the values of the pairs g1, g2 and g3 : GeeksQuiz.com We can also swap pairs (but type of pairs should be same) : Before swapping, g1 has Geeks and g2 has .com After swapping, g1 has .com and g2 has Geeks
This article is contributed by MAZHAR IMAM KHAN. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.