C Programming on the IBM PC (C Programmers Reference Guide Series)

The pair Class

The pair class is used to house pairs of objects, such as might be stored in an associative container. It has this template specification:

template <class Ktype, class Vtype> struct pair {  typedef Ktype first_type;  typedef Vtype second_type;  Ktype first;  Vtype second;  // constructors  pair();  pair(const Ktype &k, const Vtype &v);  template<class A, class B> pair(const<A, B> &ob); }

The value in first typically contains a key, and the value in second typically contains the value associated with that key.

The following operators are defined for pair: ==, !=, <, <=, >, and >=.

You can construct a pair using either one of pair’s constructors or by using make_pair( ), which constructs a pair object based upon the types of the data used as parameters. make_pair( ) is a generic function that has this prototype:

      template <class Ktype, class Vtype>          pair<Ktype, Vtype> make_pair(const Ktype &k, const Vtype &v);

It returns a pair object consisting of values of the types specified by Ktype and Vtype. The advantage of make_pair( ) is that the types of the objects being stored are determined automatically by the compiler rather than being explicitly specified by you.

The pair class and the make_pair( ) function require the header <utility>.

Категории