Open In App

Pair in C++ STL

Last Updated : 25 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Output
1: Geeks

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

Output
1 Geeks
2 ForGeeks

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

Output
1 Geeks

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

Output
2 ForGeeks

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:

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.

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

Output
p1 == 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;
}

Output
First 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.


Next Article
Practice Tags :

Similar Reads