Fixed-Size Heterogenous Collections
Get introduced to fixed-size heterogeneous collections with std::pair and std::tuple, accessing and iterating their members.
We'll cover the following
The C++ Utility library includes two class templates that can be used for storing multiple values of different types: std::pair
and std::tuple
. They are both collections with a fixed size. Like std::array
, adding more values dynamically at runtime is impossible.
The big difference between std::pair
and std::tuple
is that std::pair
can only hold two values, whereas std::tuple
can be instantiated with an arbitrary size at compile time. We will begin with a brief introduction to std::pair
before moving on to std::tuple
.
Using std::pair
The class template std::pair
lives in the <utility>
header and has been available in C++ since the introduction of the standard template library. It is used in the standard library where algorithms need to return two values, such as std::minmax()
, which can return both the smallest and the greatest value of an initializer list:
Get hands-on with 1200+ tech skills courses.