Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Containers

C++ std::pair class

Reference for std::pair: two values of possibly different types with first and second, make_pair, comparison, structured bindings and use in maps.

std::pair<T1, T2> (header <utility>) bundles exactly two values, possibly of different types, into one object. The members are public and named first and second. Pairs are what associative containers store: a std::map<K, V> holds std::pair<const K, V> elements, which is why iterating a map gives you it->first and it->second.

A pair is also the lightest way to return two values from a function. For anything that is going to be read in several places, a small named struct is clearer, because first and second say nothing about the meaning of the values.

C++
#include <utility>

std::pair<std::string, int> entry{"age", 31};

Try it

C++ Creating, reading and comparing pairs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Pairs in maps and vectors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template<class T1, class T2> struct pair {
    T1 first;
    T2 second;
    constexpr pair();
    constexpr pair(const T1& a, const T2& b);
    template<class U1, class U2> constexpr pair(U1&& a, U2&& b);
};

template<class T1, class T2>
constexpr std::pair<std::decay_t<T1>, std::decay_t<T2>> make_pair(T1&& a, T2&& b);
Name Description
first, second The two public data members.
a, b Values used to initialize the members; they are moved or copied as appropriate.
std::piecewise_construct Tag that lets you build each member from its own argument list through std::tuple arguments.

Returns: make_pair returns a pair with decayed types (std::reference_wrapper is unwrapped to a reference). The default constructor value-initializes both members.

Errors / undefined behavior: none of its own. Comparing pairs requires both element types to be comparable.

Usage notes

  • Class template argument deduction (C++17) lets you write std::pair p{1, 2.5};, so make_pair is rarely needed except in generic code or to pass a pair as an argument without naming its type.
  • Comparison is lexicographic: first is compared, and second only when the firsts are equal. This makes a vector<pair<...>> sortable with no comparator (see std::sort).
  • Structured bindings (auto [a, b] = p;) copy by default; use auto& [a, b] to modify the pair's members through the names.
  • std::get<0> / std::get<1> and std::tuple_size work on pairs, so generic code can treat them like two-element tuples. std::get<T>(p) by type fails if both types are the same.
  • Map elements have a const key, so std::pair<std::string, int> is not the same type as the map's value_type, although it converts.
  • Implicit conversions: std::pair<long, double> can be built from std::pair<int, float> element by element.
  • Prefer a named struct when the two values have meanings the reader must remember (width/height beats first/second).
  • Triviality: a pair of trivial types is not itself trivially assignable in every standard library; do not memcpy pairs.