DekGenius.com
Team LiB   Previous Section   Next Section

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

make_pair function template Constructs a pair object

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

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.

Example

Example 13-39. 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(  ) : 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 Compares 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 Compares for inequality

namespace rel_ops {
  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 Compares 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 that 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 Compares for less-than-or-equal

namespace rel_ops {
  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 Compares for greater-than

namespace rel_ops {
  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.

operator>= function template Compares for greater-than-or-equal

namespace rel_ops {
  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).

pair class template Represents 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, 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<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

rel_ops namespace Relational operators

namespace std {
 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 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 using namespace std::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.

See Also

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

    Team LiB   Previous Section   Next Section