Pair is a simple container that holds two values together. These two values can be of different types, and they are stored as a single unit.
The pair container has the following applications.
- Return two values from a function
- Key‐value storage (Used in map and unordered_map)
- Provides lexicographical comparison first compares
first item, then second if first is equal and can be useful in sorting.
C++
#include <iostream>
#include <utility>
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;
}
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. Using curly braces {}
C++
#include <iostream>
#include <utility>
using namespace std;
int main()
{
pair<int, string> p1 = {1, "Apple"};
// Elements are accessed using (.) operator
cout << p1.first << " " << p1.second << endl;
return 0;
}
Note: In pair, first and second values are stored as data members. So, we can access them by using their name with (.) operator.
2. Using make_pair()
C++
#include <iostream>
#include <utility>
using namespace std;
int main()
{
pair<int, string> p2 = make_pair(2, "Banana");
cout << p2.first << " " << p2.second << endl;
return 0;
}
3. Default Constructor + Assignment
C++
#include <iostream>
#include <utility>
using namespace std;
int main()
{
pair<int, string> p3;
p3.first = 3;
p3.second = "Cherry";
cout << p3.first << " " << p3.second << endl;
return 0;
}
4. Structured bindings (C++17) - Unpacking the pair
- Structured bindings let you split a pair into two separate variables in one step, (instead of using .first and .second).
- It makes the code cleaner and easier to read, but it works only in C++17 or later.
C++
#include <iostream>
#include <utility>
using namespace std;
int main()
{
// Create a pair
pair<int, string> myPair = {1, "Geeks"};
// Unpack the pair using structured bindings
auto [number, text] = myPair;
// Now we can use number and text directly
cout << "Number: " << number << "\n";
cout << "Text: " << text << "\n";
return 0;
}
OutputNumber: 1
Text: Geeks
Operations in Pair
Accessing Values
In pair, first and second values are stored as data members. So, we can access them by using their name with (.) operator.
Update Values
We can update the values in a pair by directly changing .first and .second;
C++
#include <iostream>
#include <utility>
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;
}
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. |
|---|
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
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems