[C++] How to Use std::tuple | How to Group Multiple Values of Different Types

目次

Introduction

In C++, there are cases where you want to return multiple values from a function or temporarily handle data of several different types in a single variable. For this purpose, the std::tuple class was introduced in C++11. std::tuple can be thought of as a generalized version of std::pair, capable of holding an arbitrary number of elements (usually a large number, depending on the compiler) of arbitrary types. In this article, I will explain the basic usage of std::tuple provided in the <tuple> header, including creation, modification of values, and access to elements.

Prerequisite: What is C++11?

C++11 is a major update to the C++ language formalized in 2011. Since std::tuple is a feature introduced in C++11, you need a compiler that supports C++11 or later to use it.

Sample Code Using std::tuple

This code demonstrates how to create a tuple holding three different types of values (int, string, and double), change those values, and extract and display individual elements.

#include <iostream>
#include <tuple>   // Required to use tuple, make_tuple, get
#include <string>

using namespace std;

int main() {
    // 1. Create a tuple (initialize with constructor)
    tuple<int, string, double> product_data(101, "Apple", 120.5);

    // 2. Access the N-th element (0-based) using get<N>(tuple)
    cout << "--- Initial Values ---" << endl;
    cout << "ID: " << get<0>(product_data) << endl;
    cout << "Product Name: " << get<1>(product_data) << endl;
    cout << "Price: " << get<2>(product_data) << endl;

    // 3. Create a new tuple with make_tuple and assign it
    product_data = make_tuple(202, "Orange", 80.0);

    cout << "\n--- After Changing Values ---" << endl;
    cout << "ID: " << get<0>(product_data) << endl;
    cout << "Product Name: " << get<1>(product_data) << endl;
    cout << "Price: " << get<2>(product_data) << endl;

    return 0;
}

Code Explanation

1. Creating a tuple

tuple<int, string, double> product_data(...);

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

2. std::make_tuple

product_data = make_tuple(202, "Orange", 80.0);

std::make_tuple is a helper function that automatically generates and returns a tuple object of the corresponding type from the values passed as arguments. You can also use an initializer list like tuple<int, string, double> t = {202, "Orange", 80.0};.

3. Accessing Elements (std::get)

cout << get<0>(product_data) << endl;

To access each element of a tuple, use std::get.

  • get<N>(tuple variable): Specify the index number (0-based) of the element you want to access as a constant inside < >.
  • get<0> returns the first element (int), get<1> returns the second element (string), and get<2> returns the third element (double).

Note: The index of get must be a constant (literal value) determined at compile time, not a variable. You cannot use it like get<i> inside a for loop.

Summary

In this article, I explained how to group values of multiple different types into a single object using C++11 std::tuple.

  • You can define combinations of arbitrary types with std::tuple<T1, T2, ...>.
  • You can easily generate a tuple from values using std::make_tuple.
  • You can access the N-th element by specifying a 0-based index with std::get<N>.

tuple is very useful as a temporary data container, especially when you want to return multiple values from a function and defining a structure seems unnecessary.

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

この記事を書いた人

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

目次