-
Functor is a function object
-
Function object:
-
Overloaded operator () class objects
-
Normal function and use the same, so called function object or functor
-
-
For the parameters for the STL functor special handling, we define two special classes, class type definition there is only
-
Unary functions class, unary_function
template <class Arg, class Result> struct unary_function { typedef Arg argument_type; typedef Result result_type; };
-
- Binary function class, binary_function
template <class Arg1, class Arg2, class Result> struct binary_function { typedef Arg1 first_argument_type; typedef Arg2 second_argument_type; typedef Result result_type; }
-
- example
template <class T> struct plus : public binary_function<T, T, T> { T operator()(const T& x, const T& y) { return x + y; } }
- Card with
-
After the value of permits anyone to use the same function, get his or her own
template <class T> struct identity :public : public unary_function<T, T> { T& operator()(const T& x) const { return x; } }
-
Receiving one pair, first or second return element
template <class Pair> struct select1st : public unary_function<Pair, typedef pair::first_type> { const typename Pair::first_type& operator()(const Pair& x) const { return x.first; } } template <class Pair> struct select2nd : public unary_function<Pair, typedef pair::second_type> { const typename Pair::second_type& operator()(const Pair& x) const { return x.second; } };
- Projection function
-
Returns to the first or second parameter
-
template <class Arg1, class Arg2> struct project1st : public binary_function<Arg1, Arg2, Arg1> { Arg1 operator()(const Arg1& x, const Arg2&) const { return x; } }; template <class Arg1, class Arg2> struct project2nd : public binary_function<Arg1, Arg2, Arg2> { Arg2 operator()(const Arg1&, const Arg2& y) const { return y; } };