This lesson talks about std::tuple in detail.

Tuples extend the principles of a pair to a broader range of functions. We can create tuples of arbitrary lengths and types with std::tuple. The class template needs the header <tuple>. std::tuple is a generalization of std::pair. We can convert between tuples with two elements and pairs. The tuple has, like his younger brother std::pair, a default, a copy, and a move constructor. We can swap tuples with the function std::swap.

The i-th element of a tuple, t, can be referenced by the function template std::get: std::get<i-1>(t). By std::get<type>(t), we can directly refer to the element of the type type.

Tuples support the comparison operators ==, !=, <, >, <=, and >=. If we compare two tuples, the elements of the tuples will be compared lexicographically. The comparison starts at index 0.

std::make_tuple #

The helper function std::make_tuple is quite convenient for the creation of tuples. We don’t have to provide the types. The compiler automatically deduces them.

For a better understanding, consider the example below:

Get hands-on with 1200+ tech skills courses.