Draft 2002-08-19

<utility>

The <utility> header declares several functions that are used throughout the standard library. It also declares the pair<> class template, which has many uses, especially by maps in the <map> header.

rel_ops namespace

Relational operators

namespace rel_ops {
  template<typename T> bool operator!=(const T&, const T&);
  template<typename T> bool operator> (const T&, const T&);
  template<typename T> bool operator<=(const T&, const T&);
  template<typename T> bool operator>=(const T&, const T&);
}

The std::rel_ops namespace declares four comparison operators. The operators use the == and < operators to implement the other comparison operators. Thus, if a class provides only equivalence and less-than comparisons, you can use the rel_ops namespace to obtain all the comparison operators. When 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 definitions of these functions are trivial, so the cost is almost zero, but the benefits are greater clarity and simplicity because you do not force your users to get enmeshed in namespace lookup rules.

See Also

operator!= function template, operator> function template, operator<= function template, operator >= function template

make_pair function template

Construct a pair object

template <typename T1, typename T2>
pair<T1,T2> make_pair(T1 a, T2 b);

Constructs a pair<T1,T2> object and initalizes it with the value a and b. The advantage of using make_pair over a simple pair<> constructor is that the compiler can infer the types T1 and T2 from the values a and b. Example 13-41 shows some typical uses for make_pair.

Example 13-41: Making pairs of objects.

std::map<std::string, int> wordcounts;
wordcounts.insert(std::make_pair("hello", 1));

// Functor, suitable for passing to for_each to find
// minimum and maximum values in a range.
template<typename T>
class minmax
{
public:
  minmax_t() : min_(std::numeric_limits<T>::max()),
               max_(std::numeric_limits<T>::min())
               {}
  void operator()(const T& x) {
    if (x < min_) min_ = x;
    if (max_ < x) max_ = x;
  }
  operator std::pair<T,T>() const {
    return std::make_pair(min_, max_);
  }
private:
  T min_;
  T max_;
};

int main()
{
  std::vector<int> v;
  // fill v with data
  std::pair<int,int> mm =
    std::for_each(v.begin(), v.end(), minmax<int>());
  // do something with mm
}

See Also

pair class template

operator== function template

Compare for equality

template <typename T1, typename T2>
bool operator==(const pair<T1,T2>& a, const pair<T1,T2>& b);

Returns true if a and b are equal, that is, a.first == b.first && a.second == b.second.

operator!= function template

Compare for inequality

template<typename T> bool operator!=(const T& a, const T& b);

template <typename T1, typename T2>
bool operator!=(const pair<T1,T2>& a, const pair<T1,T2>& b);

Returns true if a and b are not equal, that is, ! (a == b).

operator<= function template

Compare for less than or equal

template<typename T> bool operator<=(const T& a, const T& b);

template <typename T1, typename T2>
bool operator<=(const pair<T1,T2>& a, const pair<T1,T2>& b);

Returns true if a is less than or equal to b, that is, ! (b < a).

operator< function template

Compare for less than

template <typename T1, typename T2>
bool operator<(const pair<T1,T2>& a, const pair<T1,T2>& b);

Returns true if a is less than b, assuming the first member is more significant than second. That is, the return value is a.first < b.first || (!b.first < a.first) && a.second < b.second).

operator>= function template

Compare for greater than or equal

template<typename T> bool operator>=(const T& a, const T& b);

template <typename T1, typename T2>
bool operator>=(const pair<T1,T2>& a, const pair<T1,T2>& b);

Returns true if a is greater than or equal to b, that is, ! (a < b).

operator> function template

Compare for greater than

template<typename T> bool operator>(const T& a, const T& b);

template <typename T1, typename T2>
bool operator>(const pair<T1,T2>& a, const pair<T1,T2>& b);

Returns true if a is greater than b, that is, b < a.

pair class template

Represent a pair of related objects

template <typename T1, typename T2>
struct pair {
  typedef T1 first_type;
  typedef T2 second_type;
  T1 first;
  T2 second;
  pair();
  pair(const T1& x, const T2& y);
  template<typename U, typename V> pair(const pair<U, V> &p);
};

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

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<typename U, typename V>
pair (const pair<U, V> &p)
Initializes first with p.first and second with p.second, performing implicit conversions as needed.

See Also

make_pair function template