Tuples in C++

Last Updated : 27 Jul, 2026

A tuple is a fixed-size container introduced in C++11 that stores multiple values together. Unlike arrays and vectors, a tuple can hold elements of different data types, making it useful for grouping related information into a single object.

  • Stores multiple values of different data types.
  • Provides compile-time indexed access to elements.
  • Commonly used for returning multiple values from functions.
C++
#include <iostream>
#include <tuple>
using namespace std;

int main() {

    tuple<int, string, float> student(
        101,
        "Alice",
        89.5
    );

    cout << get<0>(student) << " ";
    cout << get<1>(student) << " ";
    cout << get<2>(student);

    return 0;
}

Output
101 Alice 89.5

Explanation: The tuple stores an integer, a string, and a floating-point value in a single object.

Syntax

A tuple can be created using either its constructor or the make_tuple() function.

tuple<data_type1, data_type2, ...> tuple_name;
or
make_tuple(value1, value2, ...);

Accessing Tuple Elements

Tuple elements are accessed using the get<index>() function. The index starts from 0.

C++
#include <iostream>
#include <tuple>
using namespace std;

int main() {

    tuple<int, string, float> student(
        101,
        "Alice",
        89.5
    );

    cout << get<0>(student) << endl;
    cout << get<1>(student) << endl;
    cout << get<2>(student);

    return 0;
}

Output
101
Alice
89.5

Explanation: get<0>(), get<1>(), and get<2>() access the first, second, and third elements of the tuple, respectively.

Modifying Tuple Elements

The value returned by get() can also be modified.

C++
#include <iostream>
#include <tuple>
using namespace std;

int main() {

    tuple<int, string, float> student(
        101,
        "Alice",
        89.5
    );

    get<1>(student) = "Bob";
    get<2>(student) = 95.0;

    cout << get<0>(student) << " ";
    cout << get<1>(student) << " ";
    cout << get<2>(student);

    return 0;
}

Output
101 Bob 95

Explanation: The second and third elements are updated while the remaining elements remain unchanged.

Tuple Operations

Finding the Size of a Tuple

The number of elements stored in a tuple can be obtained using the tuple_size type trait. Since the size of a tuple is fixed at compile time, tuple_size returns a compile-time constant.

C++
#include <iostream>
#include <tuple>
using namespace std;

int main() {

    tuple<int, string, float> student(
        101,
        "Alice",
        89.5
    );

    cout << tuple_size<decltype(student)>::value;

    return 0;
}

Output
3

Explanation: The tuple contains three elements, so tuple_size returns 3.

Finding the Type of a Tuple Element

The tuple_element type trait is used to determine the data type of an element at a particular position in a tuple.

C++
#include <iostream>
#include <tuple>
using namespace std;

int main() {

    tuple<int, string, float> student(
        101,
        "Alice",
        89.5
    );

    tuple_element<1, decltype(student)>::type name;

    name = get<1>(student);

    cout << name;

    return 0;
}

Output
Alice

Explanation: The element at index 1 is of type string, so tuple_element resolves to string.

Swapping Two Tuples

The swap() function exchanges the contents of two tuples of the same type.

C++
#include <iostream>
#include <tuple>
using namespace std;

int main() {

    tuple<int, char, float> tup1(
        10,
        'A',
        5.5
    );

    tuple<int, char, float> tup2(
        20,
        'B',
        8.5
    );

    tup1.swap(tup2);

    cout << get<0>(tup1) << " ";
    cout << get<1>(tup1) << " ";
    cout << get<2>(tup1);

    return 0;
}

Output
20 B 8.5

Explanation: After calling swap(), all elements of tup1 and tup2 are exchanged.

Unpacking a Tuple Using tie()

The tie() function assigns the values stored in a tuple to separate variables.

C++
#include <iostream>
#include <tuple>
using namespace std;

int main() {

    tuple<int, string, float> student(
        101,
        "Alice",
        89.5
    );

    int id;
    string name;
    float marks;

    tie(id, name, marks) = student;

    cout << id << " ";
    cout << name << " ";
    cout << marks;

    return 0;
}

Output
101 Alice 89.5

Explanation: The tuple elements are unpacked into individual variables in the same order.

Concatenating Tuples

The tuple_cat() function combines two or more tuples into a single tuple.

C++
#include <iostream>
#include <tuple>
using namespace std;

int main() {

    tuple<int, char> tup1(
        10,
        'A'
    );

    tuple<float, string> tup2(
        7.5,
        "Bob"
    );

    auto tup3 = tuple_cat(tup1, tup2);

    cout << get<0>(tup3) << " ";
    cout << get<1>(tup3) << " ";
    cout << get<2>(tup3) << " ";
    cout << get<3>(tup3);

    return 0;
}

Output
10 A 7.5 Bob

Explanation: tuple_cat() creates a new tuple containing all elements of the input tuples while preserving their order.

Applications of Tuple

Tuples are commonly used when multiple related values need to be stored or transferred together without creating a custom class or structure.

  • Return multiple values from a single function.
  • Group related values of different data types.
  • Store temporary data in STL algorithms.

Advantages of Tuple

Tuples provide a simple and efficient way to manage heterogeneous data in C++.

  • Stores multiple values of different data types.
  • Reduces the need for custom structures or classes.
  • Supports efficient access using compile-time indexing.

Limitations of Tuple

Although tuples are flexible, they have a few limitations compared to user-defined data types.

  • Elements are accessed using numeric indices only.
  • Less readable than structures with named members.
  • Size and data types cannot change after creation.
Comment