13.49 <utility>

The <utility> header declares the
pair<> class template, which has many uses,
especially by maps in the <map> header. It
also defines the rel_ops namespace, which defines
relational operators in terms of == and
<.

Constructs a pair<T1,T2> object and
initializes it with the values a and
b. The advantage of using
make_pair over a simple
pair<> constructor is that the compiler can
deduce the types T1 and T2 from
the values a and b. Example 13-39 shows a typical use of
make_pair.

The pair class template represents a pair of
related objects, in which the relationship is defined by the
programmer. The most common use of pairs is by the
map class template, which stores pairs of keys and
associated objects.

The Boost project has a generalization of pair,
called tuple. See Appendix B for
information about Boost.

The pair constructors are straightforward:

pair( )

Initializes first as T1( ) and
second as T2( )

pair(const T1& x, const T2& y)

Initializes first with x and
second with y

template<typenameU, typenameV>

pair(constpair<U, V>&p)

Initializes first with p.first
and second with p.second,
performing implicit conversions as needed

The std::rel_ops namespace declares four
comparison operators. The four operators are implemented in terms of
the == and < operators. The
rel_ops namespace has limited utility. If you are
using an unusual class, which has only operator==
and operator<, you can add a
usingnamespacestd::rel_ops directive to a function that makes
heavy use of comparison operators and this unusual class. Even
better, though, is fixing the class declaration to provide all
necessary comparison operators. If you are writing a class that
represents an ordered value, you should provide all six operators and
not force your users to rely on rel_ops. The Boost
project has templates that you can derive from to fill in all the
relational operators, based on equality and less-than. See Appendix B for information about Boost.