Fixed size heterogenous collections
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. Just like std::array
, it's not possible to add more values dynamically at runtime.
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:
std::pair<int, int> v = std::minmax...