Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::unary_function

 

std::unary_function is an empty class that serves as the base class of a unary functor. A unary functor defines operator(), where operator() takes one argument. The advantage of using std::unary_function is its many typedefs, so that the derived functors fit in more algorithms.

 

 

 

 

 

Example: MakeAbs

 

#include <algorithm>
#include <cmath>
#include <functional>
#include <vector>

template <class T> struct Abs : public std::unary_function<T,T>
{
  const T operator()(const T& x) const { return std::abs(x); }
};

void MakeAbs(std::vector<int>& v)
{
  std::transform(v.begin(),v.end(),v.begin(),Abs<int>());
}

 

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict