Pair in C++ STL

Last Updated : 19 Jan, 2026

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(){

    pair<int, string> p1 = {1, "Geeks"};
    cout << p1.first << " : " << p1.second;
    return 0;
}

Output
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. Using curly braces {}

C++
#include <iostream>
#include <utility>
using namespace std;

int main(){
    pair<int, string> p1 = {1, "Apple"};
    cout << p1.first << " " << p1.second << endl;
    return 0;
}

Output
1 Apple

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;
}

Output
2 Banana

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;
}

Output
3 Cherry

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(){

    pair<int, string> myPair = {1, "Geeks"};

    auto [number, text] = myPair;
    cout << "Number: " << number << "\n";
    cout << "Text: " << text << "\n";
    return 0;
}

Output
Number: 1
Text: Geeks

Operations in Pair

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.

For example:

p.first // returns first value
p.second // returns second value

2. 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"};

    p.first = 2;
    p.second = "ForGeeks";
    cout << p.first << " " << p.second;
    return 0;
}

Output
2 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:

OperatorDescription
==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};

    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;
}
Try It Yourself
redirect icon

Output
p1 == p2: 0
p1 != p3: 1
p1 > p3: 1
p1 < p2: 1
p1 >= p3: 1
p3 <= p1: 1
Comment