In C++, pair is used to combine together two values that may be of different data types or same data types as a single unit. The first element is stored as a data member with name 'first' and the second element as 'second'.
Example:
CPP
#include <bits/stdc++.h>
using namespace std;
int main() {
// Creating a pair of int and string
pair<int, string> p1 = {1, "Geeks"};
cout << p1.first << ": " << p1.second;
return 0;
}
Explanation: In this program, we created a pair p1 of type int and string with the values {1, "Geeks"}.
Syntax
The pair container is defined in <utility> header file.
pair <T1, T2> p;
where,
- T1: Data type of the first element.
- T2: Data type of the second element.
- p: Name assigned to the pair.
Declaration and Initialization
In C++, pair can be declared and initialized in multiple ways as shown below:
1. Default Initialization
We can declare an empty pair using the below declaration:
pair <T1, T2> p;
2. Declaration and Initialization with Values
We can initialize a pair directly by assigning values to first and second.
pair<T1, T2> p = {v1, v2};
3. Initialization with make_pair()
We can use make_pair() method to initialize pair.
pair<T1, T2> p = make_pair(v1, v2);
Let's take simple example to demonstrate all ways:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
// Creating an empty pair
pair<int, string> p1;
// Insert values using curly braces {}
pair<int, string> p2 = {1, "Geeks"};
// Insert values using make_pair method
pair<int, string> p3 = make_pair(2, "ForGeeks");
cout << p2.first << " " << p2.second << endl;
cout << p3.first << " " << p3.second;
return 0;
}
Explanation: In the above program, we created three pairs:
- pair<int, string> p1 creates an empty pair of type int and string.
- pair<int, string> p2 = {1, "Geeks"} creates a pair and initializes it with the values {1, "Geeks"} using curly braces.
- pair<int, string> p3 = make_pair(2, "ForGeeks") creates a pair and initializes it with the values {2, "ForGeeks"} using the make_pair method.
All the values should match the type of the pair, otherwise, a compiler error will be displayed.
Note: If a pair is not initialized, the compiler automatically assigns the first and second members default values according to their types.
Basic Operations
The basic operations on pairs are show below:
1. Accessing Values
In pair, first and second values are stored as data members. So, we can access them by using their name with (.) operator.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
pair<int, string> p = {1, "Geeks"};
// Accessing the elements of the pair
cout << p.first << " " << p.second;
return 0;
}
Explanation: In the above program, p.first accesses the first values of the pair, which is 1. p.second accesses the second element of the pair, which is "Geeks".
2. Update Values
We update the elements of pair like accessing elements from pair but in place of access, we just assign new value using assignment operator.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
pair<int, string> p = {1, "Geeks"};
// Update first and second value of pair
p.first = 2;
p.second = "ForGeeks";
cout << p.first << " " << p.second;
return 0;
}
Explanation: In the above code, p.first = 2 directly changes the first value of the pair to 2, and p.second = "ForGeeks"; directly changes the second value of the pair to "ForGeeks".
3. Compare Pairs
Just like other data types, we can use relational operators with pairs. They initially compare the first value. If it does not give result, then second value is compared. The following table describes the behaviour of these operators for pairs:
Operator | Description |
---|
== | Returns true if both pairs are equal, otherwise false. |
---|
!= | Returns true if pairs are not equal, otherwise false. |
---|
> | Returns true if the LHS pair is greater than the RHS pair, otherwise false. |
---|
< | Returns true if the left operand is less than the right operand, otherwise false. |
---|
>= | Returns true if the left operand is greater than or equal to the right operand, otherwise false. |
---|
<= | Returns true if the left operand is less than or equal to the right operand, otherwise false. |
---|
Example:
C++
#include <iostream>
using namespace std;
int main() {
pair<int, int> p1 = {3, 5};
pair<int, int> p2 = {3, 7};
pair<int, int> p3 = {2, 5};
// printing result of comparision
cout << boolalpha;
cout << "p1 == p2: " << (p1 == p2) << endl;
cout << "p1 != p3: " << (p1 != p3) << endl;
cout << "p1 > p3: " << (p1 > p3) << endl;
cout << "p1 < p2: " << (p1 < p2) << endl;
cout << "p1 >= p3: " << (p1 >= p3) << endl;
cout << "p3 <= p1: " << (p3 <= p1);
return 0;
}
Outputp1 == p2: false
p1 != p3: true
p1 > p3: true
p1 < p2: true
p1 >= p3: true
p3 <= p1: true
Unpacking a Pair
We can store extract and store the two values of the pair in two different variables of same type using tie() function.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
pair<int, string> p = {1, "Geeks"};
// Variables to store extracted values
int a;
string s;
// Extracting values using tie()
tie(a, s) = p;
cout << "First value: " << a << endl;
cout << "Second value: " << s;
return 0;
}
OutputFirst value: 1
Second value: Geeks
Explanation: In this program, the first and second value of the pair p is extracted into the variable a and s using the function tie().
Common Applications of pairs
A pair is commonly used for the following purposes:
- Returning multiple values from functions.
- Storing key-value pairs in other containers, especially maps.
- Sorting containers on the basis of multiple criteria.
Similar Reads
make_pair() in C++ STL In C++, make_pair() is a standard library function used to construct a key-value pair from the given arguments. The type of the pair constructed is deduced automatically from the type of arguments. In this article, we will learn about make_pair() function in C++.Letâs take a quick look at a simple e
3 min read
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 cont
3 min read
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 cont
3 min read
List in C++ STL In C++, list container implements a doubly linked list in which each element contains the address of next and previous element in the list. It stores data in non-contiguous memory, hence providing fast insertion and deletion once the position of the element is known.Example:C++#include <iostream
7 min read
set operator= in C++ STL The â=â is an operator in C++ STL which copies (or moves) a set to another set and set::operator= is the corresponding operator function. There are three versions of this function: The first version takes reference of an set as an argument and copies it to an set. Syntax: ums1.operator=(set &set
2 min read
list::operator= in C++ STL Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::operator= This operator is used to assign n
2 min read