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, ...

Get Beginning C++ Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.