STL 函数对象

来源:互联网 发布:互联网软件行业周期 编辑:程序博客网 时间:2024/05/17 23:35

stl_function.h


基本类:

template <class Arg, class Result>

  struct unary_function {

    typedef Arg argument_type;

    typedef Result result_type;

  };

template <class Arg1, class Arg2, class Result>

  struct binary_function {

    typedef Arg1 first_argument_type;

    typedef Arg2 second_argument_type;

    typedef Result result_type;

  };

Arithmetic operations:

  • plus     +
template <class _Tp>
struct plus : public binary_function<_Tp,_Tp,_Tp> {
  _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x + __y; }

}; //如 std::plus<int>()

  • minus    -
  • multiplies ×
  • divides ÷
  • modulus  %
  • negate   取反


Comparison operations:

  • equal_to       =
  • not_equal_to   !=
  • greater        >
  • less           <

template <class T> struct less : binary_function <T,T,bool> {

  bool operator() (const T& x, const T& y) const

    {return x<y;}

};

  • greater_equal  >=
  • less_equal     <=

Logical operations:
  • logical_and    &&
  • logical_or     ||
  • logical_not    !
适配器,转变函数对象,函数对象类
  • not1  --- 入参为一个一元判断函数,返回unary_negate函数对象类,此函数对象内存储not1的入参一元函数指针。
  • not2  --- 入参为一个二元判断函数,返回binary_negate函数对象类,此函数对象内存储not2的入参二元函数指针。
  • bind1st --- 返回binder1st 函数对象类
  • bind2nd --- 返回binder2nd函数对象类


//构造函数的入参为"二元函数和二元函数的第一个入参",为一元函数对象
binder1st ---------- bind1st的返回值
//构造函数的入参为"二元函数和二元函数的第二个入参",为一元函数对象

binder2nd -------- bind2nd的返回值

  • ptr_fun把函数指针变为函数对象 --- 使用了pointer_to_unary_function和pointer_to_binary_function函数对象;
  • mem_fun把成员函数指针变为函数对象,此函数对象的入参为此class类型的指针;由mem_fun生成以下四个:

    • mem_fun_t
    • mem_fun1_t
    • const_mem_fun_t
    • const_mem_fun1_t

  • mem_fun_ref把成员函数指针变为函数对象,此函数对象的入参为此class类型的引用;由mem_fun_ptr生成以下四个:

    • mem_fun_ref_t
    • mem_fun1_ref_t
    • const_mem_fun_ref_t
    • const_mem_fun1_ref_t


          代码细节

          template <class _Ret, class _Tp> 

          • 一元函数对象类;
          •  _Ret为返回值;_Tp为class类型; 
          • 可以通过mem_fun获取此函数对象
          • 举例:vector <string*> numbers;  transform (numbers.begin(), numbers.end(), lengths.begin(),mem_fun(&string::length)); 会依次执行numbers对象的length成员函数。
          • vector<string> numbers;  transform (numbers.begin(), numbers.end(), lengths.begin(),mem_fun_ref(&string::length));
          • mem_fun可以返回四种类型的函数对象:
            • template <class S, class T> mem_fun_t<S,T> mem_fun (S (T::*f)());
            • template <class S, class T, class A> mem_fun1_t<S,T,A> mem_fun (S (T::*f)(A));
            • template <class S, class T> const_mem_fun_t<S,T> mem_fun (S (T::*f)() const);
            • template <class S, class T, class A> const_mem_fun1_t<S,T,A> mem_fun (S (T::*f)(A) const);

          class mem_fun_t : public unary_function<_Tp*,_Ret> { 

          public:

            explicit mem_fun_t(_Ret (_Tp::*__pf)()) : _M_f(__pf) {}

            _Ret operator()(_Tp* __p) const { return (__p->*_M_f)(); }-------此函数对象的()有入参,入参为class类型的指针

          private:

            _Ret (_Tp::*_M_f)();

          };

          template <class _Ret, class _Tp>

          class const_mem_fun_t : public unary_function<const_Tp*,_Ret> {

          public:

            explicit const_mem_fun_t(_Ret (_Tp::*__pf)() const) : _M_f(__pf) {}

            _Ret operator()(const _Tp* __p) const { return (__p->*_M_f)(); }

          private:

            _Ret (_Tp::*_M_f)() const;

          };

          template <class _Ret, class _Tp>

          class mem_fun_ref_t : public unary_function<_Tp,_Ret> {

          public:

            explicit mem_fun_ref_t(_Ret (_Tp::*__pf)()) : _M_f(__pf) {}

            _Ret operator()(_Tp& __r) const { return (__r.*_M_f)(); }--- 此处不是指针,是引用

          private:

            _Ret (_Tp::*_M_f)();

          };


          • mem_fun_ref把成员函数指针变为函数对象,此函数对象的入参为此class类型的引用;