Understanding std::pair in the C++ Standard Library

The std::pair is a template class defined in the <utility> header file. It enables combining two values into a single object, which is extensively used throughout the C++ standard library. Containers like std::map, std::unordered_map, and std::unordered_multimap rely on pairs to store key-value associations. Additionally, functions such as std::minmax() utilize pairs to return multiple values.

Internally, std::pair is a struct defined with in the std namespace with public members:

namespace std {
    template <typename T1, typename T2>
    struct pair {
        T1 first;   // first element
        T2 second;  // second element
    };
}

Member Functions and Operations

Construction and Initialization

std::pair<T1, T2> p; — Creates an empty pair with default-initialized members std::pair<T1, T2> p(val1, val2); — Creates a pair initialized with specified values std::pair<T1, T2> p2(p1); — Copy constructor creating p2 from p1 p2 = p1; — Assignment operator copying p1 to p2 std::make_pair(val1, val2); — Factory function returning a pair object

Element Access

p.first — Access the first element p.second — Access the second element std::get<0>(p) — Alternative to p.first std::get<1>(p) — Alternative to p.second

Comparison Operators

p1 == p2 — Returns true if both elements match p1 != p2 — Returns true if any element differs p1 > p2, p1 < p2, p1 >= p2, p1 <= p2 — Lexicographic comparison (first compared first, then second if first elements are equal)

Utility

p1.swap(p2) — Swaps the contents of two pairs

Code Examples

Basic pair operations demonstration:

#include <utility>
#include <iostream>

int main() {
    std::pair<int, double> a;
    std::cout << "a = (" << a.first << ", " << a.second << ")\n";

    std::pair<int, double> b(42, 3.14);
    std::cout << "b = (" << b.first << ", " << b.second << ")\n";

    std::pair<int, double> c(b);
    c.first = 99;
    std::cout << "c = (" << c.first << ", " << c.second << ")\n";

    a = c;
    std::cout << "After a = c:\n";
    std::cout << "a = (" << a.first << ", " << a.second << ")\n";

    c = std::make_pair(77, 88.9);
    std::cout << "Using get<> accessor: ("
              << std::get<0>(c) << ", "
              << std::get<1>(c) << ")\n";

    return 0;
}

Practical application: returning multiple values from a function

#include <utility>
#include <iostream>

std::pair<int, int> findBoundary(int* numbers, int size) {
    int minimum = numbers[0];
    int maximum = numbers[0];

    for (int i = 1; i < size; ++i) {
        if (numbers[i] < minimum) {
            minimum = numbers[i];
        }
        if (numbers[i] > maximum) {
            maximum = numbers[i];
        }
    }
    return std::make_pair(minimum, maximum);
}

int main() {
    int data[] = {5, 12, 3, 87, 21, 9, 42, 16};
    auto result = findBoundary(data, sizeof(data) / sizeof(data[0]));

    std::cout << "Range: [" << result.first
              << ", " << result.second << "]\n";

    return 0;
}

The std::pair template provides a straightforward mechanism for grouping related values, making code cleaner when handling operations that require multiple return values or key-value associations.

Tags: C++ STL standard library pair utility

Posted on Sun, 10 May 2026 21:12:38 +0000 by ditusade