C++20 sections · 1024 units
Open in Course

Creating Tuples

Construction syntax

Tuples hold a fixed number of values of potentially different types. Create them with make_tuple or brace initialization. Example: auto t = make_tuple(1, 3.14, "hello"); This creates a tuple with int, double, and string.

Or use tuple<int, double, string> t{1, 3.14, "hello"}; Tuples are useful when you need to group more than two values but don't want to define a struct. They're common for returning multiple values from functions.

Unlike pairs, tuples can hold any number of elements. tuple<int, int, int, int> works fine. Access elements with get<0>(t), get<1>(t), etc.