[C++] How to Use std::pair | How to Handle Two Values as a Pair

目次

Introduction

When programming in C++, you often encounter data that needs to be treated as a pair, such as “key and value,” “first name and last name,” or “x-coordinate and y-coordinate.” For this purpose, the C++ standard library <utility> provides a convenient class template called std::pair.

By using std::pair, you can handle two values as a single variable, making it easy to pass them as function arguments, return values, or elements of a container. Notably, elements of std::map are stored internally as std::pair.

In this article, I will explain the basic usage of std::pair (creation, modification of values, and access to elements).

Sample Code Using std::pair

This code demonstrates how to create a pair holding an int (ID) and a string (Name), change its values, and extract/display individual elements.

#include <iostream>
#include <utility> // Required to use pair, make_pair
#include <string>

using namespace std;

int main() {
    // 1. Creation of pair (Initialization via constructor)
    pair<int, string> user_data(101, "Sato");

    // 2. Access each element with .first and .second
    cout << "--- Initial Values ---" << endl;
    cout << "ID: " << user_data.first << endl;
    cout << "Name: " << user_data.second << endl;

    // 3. Create a new pair with make_pair and assign it
    user_data = make_pair(202, "Suzuki");

    cout << "\n--- After Changing Values ---" << endl;
    cout << "ID: " << user_data.first << endl;
    cout << "Name: " << user_data.second << endl;
    
    // Using Structured Binding (C++17 or later) for convenient decomposition
    auto [id, name] = user_data;
    cout << "\n--- Structured Binding ---" << endl;
    cout << "ID: " << id << ", Name: " << name << endl;

    return 0;
}

Code Explanation

1. Generating a pair

pair<int, string> user_data(101, "Sato");

To use std::pair, specify the two data types you want to pair inside < > in order. You can generate an object by passing initial values corresponding to those types as arguments to the constructor.

2. std::make_pair

user_data = make_pair(202, "Suzuki");

std::make_pair is a helper function that automatically generates and returns a pair object of the corresponding types from the two values passed as arguments. You can also use an initializer list like pair<int, string> p = {202, "Suzuki"};.

3. Accessing Elements (.first, .second)

To access each element of a pair, use the member variables .first and .second.

  • .first: Accesses the first element.
  • .second: Accesses the second element.

Summary

In this article, I explained how to group two values into a single object using std::pair in C++.

  • You can define a pair of two types with std::pair<T1, T2>.
  • You can easily generate a pair from values using std::make_pair.
  • Access each element using .first and .second.

pair is a fundamental data structure used in various situations, such as representing key-value pairs in std::map or when you want to return two values from a function (e.g., a result and an error code).

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次