Hi everyone 
In this tutorial we will talk about Pair
So, What is Pair actually?
Pair is a simple container that stores two values (which may be of different data types) together as a single unit. The two values are stored side by side and can be accessed individually using .first and .second.
Pair is not a container like vector or deque — it is more like a small struct with two members. But because it is part of the STL and is heavily used together with other containers (like map, set, priority queue), it is good to know it well.
Header File Inclusion for pair
#include <utility>
Pair is also included automatically when we include <map>, <queue>, or other containers that use it internally. So most of the time we don’t even need to include <utility> separately.
Declaration & Initialization
Declaring an empty pair —
pair <int, int> couplePair;
The two values inside the pair will be default initialized (in this case, both will be 0).
Declaring a pair with values using constructor —
pair <int, int> couplePair(10, 20);
After Initialization couplePair will look like this —
| first | second |
|---|---|
| 10 | 20 |
Declaring a pair using make_pair() —
make_pair() is a helper function that creates a pair without us having to write the data types explicitly —
pair <int, string> studentPair;
studentPair = make_pair(101, "Imran");
After Initialization studentPair will look like this —
| first | second |
|---|---|
| 101 | Imran |
Declaring a pair using curly braces —
From C++11 onwards, we can also use curly braces to initialize a pair —
pair <int, int> couplePair = { 50, 75 };
After Initialization couplePair will look like this —
| first | second |
|---|---|
| 50 | 75 |
Copying one pair to another —
pair <int, int> pairOne = { 10, 20 };
pair <int, int> pairTwo = pairOne;
Now pairTwo will have the same values as pairOne.
| first | second |
|---|---|
| 10 | 20 |
Accessing the elements
A pair has two public members — first and second. We can access them directly using the dot operator.
Accessing first —
pair <int, string> studentPair = { 101, "Imran" };
cout << studentPair.first << endl;
Output will be – 101
Accessing second —
cout << studentPair.second << endl;
Output will be – Imran
Modifying the values
Since first and second are public, we can directly assign new values to them —
pair <int, string> studentPair = { 101, "Imran" };
studentPair.first = 202;
studentPair.second = "Linkon";
After modification studentPair will look like this —
| first | second |
|---|---|
| 202 | Linkon |
Comparing two pairs
Pair supports all comparison operators — ==, !=, <, >, <=, >=.
When comparing two pairs, the first elements are compared first. If they are equal, then the second elements are compared.
pair <int, int> pairOne = { 10, 20 };
pair <int, int> pairTwo = { 10, 30 };
if (pairOne < pairTwo)
{
cout << "pairOne is smaller" << endl;
}
else
{
cout << "pairTwo is smaller" << endl;
}
Output will be – pairOne is smaller
Here both first elements are 10, so the second elements (20 and 30) are compared. Since 20 < 30, pairOne is smaller.
pair <int, int> pairOne = { 10, 50 };
pair <int, int> pairTwo = { 20, 5 };
if (pairOne < pairTwo)
{
cout << "pairOne is smaller" << endl;
}
Output will be – pairOne is smaller
Here the first elements decide the result. Since 10 < 20, the second elements are not even checked.
Swapping
swap() method
STL pair has a method named swap(), which swaps the values between two pairs —
pair <int, int> pairOne = { 10, 20 };
pair <int, int> pairTwo = { 50, 75 };
pairOne will look like this —
| first | second |
|---|---|
| 10 | 20 |
pairTwo will look like this —
| first | second |
|---|---|
| 50 | 75 |
Now, let’s swap the values of two pairs —
// swapping values between two pairs
pairOne.swap(pairTwo);
After Swapping pairOne will look like this —
| first | second |
|---|---|
| 50 | 75 |
After Swapping pairTwo will look like this —
| first | second |
|---|---|
| 10 | 20 |
Nested Pair
A pair can also hold another pair inside it. This is useful when we need to store three (or more) related values together.
pair <int, pair<int, int> > nestedPair;
nestedPair = make_pair(5, make_pair(10, 20));
After Initialization nestedPair will look like this —
| first | second.first | second.second |
|---|---|---|
| 5 | 10 | 20 |
Accessing values from a nested pair —
cout << nestedPair.first << endl;
cout << nestedPair.second.first << endl;
cout << nestedPair.second.second << endl;
Output will be —
5
10
20
Pair with different data types
The two values inside a pair don’t need to be of the same type. They can be any combination of types —
pair <string, double> productPair = { "Pen", 25.50 };
pair <char, bool> flagPair = { 'A', true };
pair <int, vector<int> > listPair = { 1, {10, 20, 30} };
This flexibility is what makes pair so useful in practice.
Time Complexity
| Operation | Description | Time Complexity |
|---|---|---|
| make_pair() | Creates a pair from two values | O(1) |
| .first / .second | Access the first or second value | O(1) |
| = (assignment) | Assigns one pair to another | O(1) |
| ==, !=, <, >, <=, >= | Compares two pairs | O(1) |
| swap() | Swaps the values of two pairs | O(1) |
Applications
-
-
-
- Storing key-value data (like in map and unordered_map)
- Returning two values from a function
- Storing coordinates (x, y) in 2D problems
- Storing edges in graph problems (with nested pair: weight, source, destination)
- Used internally by containers like map, set, and priority queue
-
-
In the next tutorial we will talk about another useful STL container.
Until then
Happy Coding