Pairs in C++ are containers that allow us to store two values as a single element. The values may or may not be of the same data type. Pairs store values as {first, second}, the order of values is fixed. The values can be accessed using the dot operator followed by keywords. The first value can be accessed using the first keyword and the second value can be accessed using the second keyword. Pair is present in #include<utility> header file. They are useful when we have to return two values from a function.
Pair Declaration
Syntax:
pair<data_type1, data_type2> name(initial_values);
data_type1: datatype of the first value
data_type2: datatype of the second value
initial_values: optional parameter which initializes the pair with the given values
Example:
pair <int, string> p; //initializes a pair which has first value as integer and second value as string
pair <int, string> p(1, "one); //initializes a pair with initial values as {1, one}Operators on pairs
=: (Assignment operator) It assigns value to a pair. The first value of the given pair is assigned to the first value of the pair to be created and similarly for the second value.
==: (equality operator) It compares two pairs. The first value of the first pair is compared with the first value of the second pair and similarly for the second value. It returns true if both pairs contain the same first and second values.
!=: (inequality operator) It also compares two pairs. The first value of the first pair is compared with the first value of the second pair and similarly for the second value. It returns true if both pairs do not contain the same values.
<=or>=: (less than and greater than operators) They compare only the first value of the pairs and return a boolean value.
Functions on pairs
make_pair(value1, value2): It creates a new pair with the given values.
Parameters: the values that need to be added in the pair
Return type: pair object
swap(pair): It swaps the content of the pair with the given pair. The pairs that need to be swapped must be of the same data type.
Parameters: pair
Return type: void
#include <utility>
#include <iostream>
using namespace std;
int main() {
// your code goes here
pair <int, int> pair1;
pair1.first = 2;
pair1.second = 10;
pair <int, int> pair2 = make_pair(4, 12);
pair <int, string> pair3(3, "three");
pair <int, int> pair4(5, 18);
cout << "First pair is " << pair1.first << " " << pair1.second << '\n';
cout << "Second pair is " << pair2.first << " " << pair2.second << '\n';
cout << "Third pair is " << pair3.first << " " << pair3.second << '\n';
cout << "Fourth pair is " << pair4.first << " " << pair4.second << '\n';
cout << "Operators on pair, 1 represent true, 0 represent false\n";
cout << "After performing pair1 <= pair2, result is " << (pair1 <= pair2) << '\n';
cout << "After performing pair1 >= pair2, result is " << (pair1 >= pair2) << '\n';
cout << "After performing pair1 == pair2, result is " << (pair1 == pair2) << '\n';
cout << "After performing pair1 != pair2, result is " << (pair1 != pair2) << '\n';
pair1 = pair4;
cout << "After performing pair1 = pair4, contents of pair1: " << pair1.first << " " << pair1.second << '\n';
cout << "Use of swap function with pair\n";
cout << "Before swapping:\n";
cout << "Contents of pair1 = " << pair1.first << " " << pair1.second << "\n";
cout << "Contents of pair2 = " << pair2.first << " " << pair2.second << "\n";
pair1.swap(pair2);
cout << "After swapping:\n";
cout << "Contents of pair1 = " << pair1.first << " " << pair1.second << "\n";
cout << "Contents of pair2 = " << pair2.first << " " << pair2.second << "\n";
return 0;
}Output
First pair is 2 10
Second pair is 4 12
Third pair is 3 three
Fourth pair is 5 18
Operators on pair, 1 represent true, 0 represent false
After performing pair1 <= pair2, result is 1
After performing pair1 >= pair2, result is 0
After performing pair1 == pair2, result is 0
After performing pair1 != pair2, result is 1
After performing pair1 = pair4, contents of pair1: 5 18
Use of swap function with pair
Before swapping:
Contents of pair1 = 5 18
Contents of pair2 = 4 12
After swapping:
Contents of pair1 = 4 12
Contents of pair2 = 5 18