STL adapter function (function adapter)

Function adapter (function adapter): by binding, different combinations and the ability to modify the function of the adapter, can achieve powerful, with the STL generic algorithms perform complex functions.

Bind (bind)

template <class _Operation> 
class binder1st
  : public unary_function<typename _Operation::second_argument_type,
                          typename _Operation::result_type> {
protected:
  _Operation op;
  typename _Operation::first_argument_type value;
public:
  binder1st(const _Operation& __x,
            const typename _Operation::first_argument_type& __y)
      : op(__x), value(__y) {}
  typename _Operation::result_type
  operator()(const typename _Operation::second_argument_type& __x) const {
    return op(value, __x); 
  }
};

template <class _Operation, class _Tp>
inline binder1st<_Operation> 
bind1st(const _Operation& __fn, const _Tp& __x) 
{
  typedef typename _Operation::first_argument_type _Arg1_type;
  return binder1st<_Operation>(__fn, _Arg1_type(__x));
}

bind1st function has two parameters, the parameters are bound functor __fn, and the parameter value to be bound to the functor __x. Construction in the function and returns binder1st object set and corresponding configuration parameters.

binder1st class has a functor (function object) to be bound, and a member of op parameter value, binder1st constructor with incoming functor class and parameters to be bound to class members op and initialize its value.

binder1st itself is a functor class (functor), the function defined in the class to complete the call operator in the actual function of functor (() operator). This function has a parameter __x, specify the operation of the second parameter. Then calls the constructor parameter class to be bound op, and initialized with a value and __x. The first parameter is returned object was bound to be binder1st binding parameters members of the value, in order to realize the function parameter binding.

binder1st principle and binder1st similar.

Negation (not)

template <class _Predicate>
inline unary_negate<_Predicate> 
not1(const _Predicate& __pred)
{
  return unary_negate<_Predicate>(__pred);
}

template <class _Predicate>
class unary_negate
  : public unary_function<typename _Predicate::argument_type, bool> {
protected:
  _Predicate _M_pred;
public:
  explicit unary_negate(const _Predicate& __x) : _M_pred(__x) {}
  bool operator()(const typename _Predicate::argument_type& __x) const {
    return !_M_pred(__x);
  }
};

not1 predicate represents a negation, functor passing objects, and returns the result of the incoming object opposite to a unary predicate functor object.
not2 represents a negative binary predicate, an object passing functor, functor binary predicate returns the result of the incoming object and the object opposite.

Synthesis of (compose)

Synthesis in a complex function similar mathematics, divided into one and binary synthesizing synthesis:
one yuan Synthesis: h (x) = f ( g (x))
Synthesis of Binary: h (x) = f ( g1 (x) , g2 (x))

template <class _Operation1, class _Operation2>
inline unary_compose<_Operation1,_Operation2> 
compose1(const _Operation1& __fn1, const _Operation2& __fn2)
{
  return unary_compose<_Operation1,_Operation2>(__fn1, __fn2);
}

template <class _Operation1, class _Operation2>
class unary_compose
  : public unary_function<typename _Operation2::argument_type,
                          typename _Operation1::result_type> 
{
protected:
  _Operation1 _M_fn1;
  _Operation2 _M_fn2;
public:
  unary_compose(const _Operation1& __x, const _Operation2& __y) 
    : _M_fn1(__x), _M_fn2(__y) {}
  typename _Operation1::result_type
  operator()(const typename _Operation2::argument_type& __x) const {
    return _M_fn1(_M_fn2(__x));        //在此处发挥作用
  }
};

Synthesis of Binary principles and synthetic phase type one yuan.

  binary_compose(const _Operation1& __x, const _Operation2& __y, 
                 const _Operation3& __z) 
    : _M_fn1(__x), _M_fn2(__y), _M_fn3(__z) { }
  typename _Operation1::result_type
  operator()(const typename _Operation2::argument_type& __x) const {
    return _M_fn1(_M_fn2(__x), _M_fn3(__x));
  }

Function pointer (ptr_mem)

This adapter will allow us to function as a functor general use.
If you do not use this function pointer adapters to make some packaging, it is generally not connected with the ability to function, and not the other adapter described earlier standards.

template <class _Arg, class _Result>
inline pointer_to_unary_function<_Arg, _Result> ptr_fun(_Result (*__x)(_Arg))
{
  return pointer_to_unary_function<_Arg, _Result>(__x);
}

template <class _Arg, class _Result>
class pointer_to_unary_function : public unary_function<_Arg, _Result> {
protected:
  _Result (*_M_ptr)(_Arg);
public:
  pointer_to_unary_function() {}
  explicit pointer_to_unary_function(_Result (*__x)(_Arg)) : _M_ptr(__x) {}
  _Result operator()(_Arg __x) const { return _M_ptr(__x); }
};

Adapter member function pointer (mem_fun, mem_fun_ref)

Such adapters may be used as a member function functor.
When the container is stored in a pointer or reference to the type of class, when elements in the container is treated with generic algorithms, you can use the function pointer to the virtual function members of the adapter to dynamically call the class defined in order to achieve polymorphism.

Member function pointers adapter in accordance with the number of parameters by reference or pointer to call, and whether it is a static member function can be divided into eight kinds:

Function name feature
mem_fun(S (T::*f)()) No parameter; pointer; non-const
mem_fun1(S (T::*f)(A)) There parameters; pointer; non-const
mem_fun(S (T::*f)() const) No parameter; pointer; const
mem_fun1(S (T::*f)(A) const) There are parameters; pointer; const
mem_fun_ref(S (T::*f)()) No parameter; reference; non-const
mem_fun1_ref(S (T::*f)(A)) There parameters; reference; non-const
mem_fun_ref(S (T::*f)() const) No parameter; reference; const
mem_fun1_ref(S (T::*f)(A) const) There are parameters; reference; const

Guess you like

Origin www.cnblogs.com/SupremeGIS-Developer/p/11967267.html