C++ Tuple Cheat Sheet | std::tuple, tie, and Structured Bindings

C++ Tuple Cheat Sheet | std::tuple, tie, and Structured Bindings

이 글의 핵심

Quick reference for std::tuple, tie, tuple_size, and practical examples including divmod and sorting.

Basic usage

#include <tuple>
using namespace std;

int main() {
    // generation
    tuple<int, double, string> t1(42, 3.14, "Hello");
    
    // make_tuple
    auto t2 = make_tuple(42, 3.14, "Hello");
    
    // access
    cout << get<0>(t1) << endl;  // 42
    cout << get<1>(t1) << endl;  // 3.14
    cout << get<2>(t1) << endl;  // Hello
}

Structured binding (C++17)

auto t = make_tuple(42, 3.14, "Hello");

// Unpack
auto [i, d, s] = t;

cout << i << endl;  // 42
cout << d << endl;  // 3.14
cout << s << endl;  // Hello

tie

int i;
double d;
string s;

auto t = make_tuple(42, 3.14, "Hello");

// Unpack to existing variable
tie(i, d, s) = t;

cout << i << endl;  // 42

// ignore some
tie(i, ignore, s) = t;

Practical example

Example 1: Multiple return values

tuple<int, int, int> divmod(int a, int b) {
    return {a / b, a % b, a};
}

int main() {
    auto [quotient, remainder, original] = divmod(17, 5);
    
    cout << original << " / 5 = " << quotient 
         << " ... " << remainder << endl;
    // 17 / 5 = 3 ... 2
}

Example 2: Caching function results

#include <map>

map<int, tuple<int, int>> cache;

tuple<int, int> fibonacci(int n) {
    if (n <= 1) {
        return {n, n};
    }
    
    if (cache.find(n) != cache.end()) {
        return cache[n];
    }
    
    auto [a, b] = fibonacci(n - 1);
    auto result = make_tuple(b, a + b);
    cache[n] = result;
    
    return result;
}

int main() {
    for (int i = 0; i < 10; i++) {
        auto [curr, next] = fibonacci(i);
        cout << curr << " ";
    }
    // 0 1 1 2 3 5 8 13 21 34
}

Example 3: Database row

using Row = tuple<int, string, double>;

vector<Row> queryDatabase() {
    return {
        {1, "Alice", 90.5},
        {2, "Bob", 85.0},
        {3, "Charlie", 95.5}
    };
}

int main() {
    auto rows = queryDatabase();
    
    for (const auto& [id, name, score] : rows) {
        cout << id << ": " << name << " (" << score << ")" << endl;
    }
}

Example 4: Coordinate system

using Point2D = tuple<double, double>;
using Point3D = tuple<double, double, double>;

Point2D add2D(Point2D p1, Point2D p2) {
    auto [x1, y1] = p1;
    auto [x2, y2] = p2;
    return {x1 + x2, y1 + y2};
}

double distance2D(Point2D p1, Point2D p2) {
    auto [x1, y1] = p1;
    auto [x2, y2] = p2;
    return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}

int main() {
    Point2D p1 = {0, 0};
    Point2D p2 = {3, 4};
    
    auto sum = add2D(p1, p2);
    auto [x, y] = sum;
    cout << "Sum: (" << x << ", " << y << ")" << endl;
    
    cout << "Distance: " << distance2D(p1, p2) << endl;  // 5
}

tuple_cat

auto t1 = make_tuple(1, 2);
auto t2 = make_tuple(3.14, "Hello");

// Combining tuples
auto combined = tuple_cat(t1, t2);
// tuple<int, int, double, string>

auto [a, b, c, d] = combined;
cout << a << ", " << b << ", " << c << ", " << d << endl;

tuple vs pair

// pair (2)
pair<int, string> p = {1, "Alice"};
cout << p.first << ", " << p.second << endl;

// tuple (N pieces)
tuple<int, string, double> t = {1, "Alice", 90.5};
cout << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << endl;

Frequently occurring problems

Issue 1: Index range

// ❌ Out of range
auto t = make_tuple(1, 2, 3);
// cout << get<3>(t) << endl;  // compile error

// ✅ Correct index
cout << get<0>(t) << endl;
cout << get<1>(t) << endl;
cout << get<2>(t) << endl;

Problem 2: Type inference

// ❌ Type inference failure
auto t = make_tuple(1, 2, 3);
// int x = get<int>(t);  // error: index required

// ✅ Use index
int x = get<0>(t);

Issue 3: Saving References

int x = 10;

// ❌ Copy
auto t1 = make_tuple(x);
get<0>(t1) = 20;
cout << x << endl;  // 10 (unchanged)

// ✅ See also
auto t2 = make_tuple(ref(x));
get<0>(t2) = 20;
cout << x << endl;  // 20 (changed)

tuple size

auto t = make_tuple(1, 2.0, "three");

// size
constexpr size_t size = tuple_size<decltype(t)>::value;
cout << size << endl;  // 3

// type
using T0 = tuple_element<0, decltype(t)>::type;  // int
using T1 = tuple_element<1, decltype(t)>::type;  // double
using T2 = tuple_element<2, decltype(t)>::type;  // const char*

FAQ

Q1: When to use tuple?

A:

  • Multiple return values
  • Temporary data group
  • Generic programming

Q2: tuple vs struct?

A:

  • tuple: simple, temporary
  • struct: clear name, readability

Q3: What is the performance overhead?

A: None. Optimized at compile time.

Q4: tuple vs pair?

A: Two pairs only. If there are three or more, it is a tuple.

Q5: What is tuple traversal?

A: Requires apply or template metaprogramming.

Q6: What are tuple learning resources?

A:

  • cppreference.com
  • “The C++ Standard Library”
  • “Effective Modern C++“

Good article to read together (internal link)

Here’s another article related to this topic.

  • C++ tuple | “Tuple” guide
  • C++ range-based for statement and structured binding | Modern C++ loop
  • C++ User-Defined Literals | “User Defined Literals” Guide

Practical tips

These are tips that can be applied right away in practice.

Debugging tips

  • If you run into a problem, check the compiler warnings first.
  • Reproduce the problem with a simple test case

Performance Tips

  • Don’t optimize without profiling
  • Set measurable indicators first

Code review tips

  • Check in advance for areas that are frequently pointed out in code reviews.
  • Follow your team’s coding conventions

Practical checklist

This is what you need to check when applying this concept in practice.

Before writing code

  • Is this technique the best way to solve the current problem?
  • Can team members understand and maintain this code?
  • Does it meet the performance requirements?

Writing code

  • Have you resolved all compiler warnings?
  • Have you considered edge cases?
  • Is error handling appropriate?

When reviewing code

  • Is the intent of the code clear?
  • Are there enough test cases?
  • Is it documented?

Use this checklist to reduce mistakes and improve code quality.


Keywords covered in this article (related search terms)

This article will be helpful if you search for C++, tuple, C++11, pair, multivalue, etc.


  • C++ tuple detailed guide |
  • C++ async & launch |
  • C++ Atomic Operations |
  • C++ Attributes |
  • C++ auto keyword |