Working with pairs and tuples
In many cases you will want to associate two items together; for example, an associative container allows you to create a type of array where items other than numbers are used as an index. The <utility>
header file contains a templated class called pair
, which has two data members called first
and second
.
template <typename T1, typename T2> struct pair { T1 first; T2 second; // other members };
Since the class is templated, it means that you can associate any items, including pointers or references. Accessing the members is simple since they are public. You can also use the get
templated function, so for a pair
object p
you can call get<0>(p)
rather than p.first
. The class also has a copy constructor, so that you can create an object from another object, and a move constructor. There is also a function called make_pair
that will deduce the types of the members from the parameters:
auto name_age...