How to use std::pair: in C++

std::pair is a template class in the C++ standard library representing a pair of values. It is defined in the <utility> header and is part of the standard template library (STL).

The std::pair template takes two template parameters, P1 and P2, representing the types of the two values in the pair. The class has two public member variables, first and second, of types P1 and P2, respectively. These member variables hold the values of the pair.

How it works

The purpose of std::pair is to provide a simple container for holding two values together as a single entity. While commonly used in situations where you need to return or pass around multiple values from a function, its applications extend beyond that.

One of the key use cases of std::pair is in mapping and key-value pair scenarios. In addition to mapping, std::pair is instrumental in various algorithms and data handling operations. It can represent graph edges and enable sorting based on values. It also facilitates searching for minimum or maximum values and aids in merging or combining data and a lot more.

With its versatility in handling key-value pairs, sorting, searching, and merging, std::pair proves to be a valuable tool in C++ programming.

Coding example

We can find an example of how std::pair can be effectively used for sorting and printing pairs of names and ages in the widget below.

#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
int main() {
// Create a vector 'data' to store pairs of names and ages
std::vector<std::pair<std::string, int>> data = {
{"John", 28},
{"Zuwyar", 50},
{"Harry", 30}
};
// Sort the pairs based on the second value (age)
std::sort(data.begin(), data.end(), [](const auto& a, const auto& b) {
return a.second < b.second;
});
// Print the sorted pairs
for (const auto& pair : data) {
// Access the name (first) and age (second) of each pair
std::cout << pair.first << ": " << pair.second << std::endl;
}
return 0;
}

Explanation

Lines 1–4: These lines include the necessary header files for the code, which are <iostream> for input/output, <utility> for std::pair, <vector> for std::vector, and <algorithm> for std::sort.

Lines 8–12: It defines an std::vector called data that stores std::pair objects. The std::pair objects consist of an std::string (representing a name) and an int (representing an age). The vector is initialized with three pairs using initializer list syntax.

Lines 15–17: It sorts the pairs in the data vector based on the second value (age). std::sort is used with three arguments: data.begin() and data.end() specify the range to be sorted (from the beginning to the end of the vector), and the third argument is a lambda function used for comparison. The lambda function compares two pairs (a and b) based on their second values and returns true if a.second is less than b.second.

Lines 20–23: This loop iterates over the sorted data vector and prints each pair. It uses a range-based for loop, where each element in data is accessed as pair. The name (pair.first) and age (pair.second) are printed using std::cout.

Additional functionalities

std::pair provides various member functions and operators for accessing and manipulating the pair, such as operator==, operator!=, operator<, operator>, operator<=, operator>=, make_pair, etc.

It’s worth noting that C++17 introduced std::make_pair, a convenient function template that deduces the types of its arguments and creates a std::pair object.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved