Pair in C++ STL (original) (raw)

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.

#include #include using namespace std;

int main(){

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

}

`

**Syntax:

The pair container is defined in header file.

pair <T1, T2> p;

where,

Declaration and Initialization

In C++, pair can be declared and initialized in multiple ways as shown below:

1. Using curly braces {}

C++ `

#include #include using namespace std;

int main(){ pair<int, string> p1 = {1, "Apple"}; 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 #include 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 #include 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

#include #include 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 #include using namespace std;

int main(){ pair<int, string> p = {1, "Geeks"};

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

}

`

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.

C++ `

#include 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 Yourselfredirect icon

Output

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