Effective STL 學習筆記 39 ~ 41

1 Make Predicate pure Function

純函數 (Pure Function) 是指輸出僅在輸入變化時才發生變化的的函數,換句話說,該類型函數的輸出不依賴於輸入以外的任何東西,例如自身狀態或者全局變量。這也是 Functional Programming 中的一個重要概念。 C++ 中用於 STL 算法的 Functor 是一些 Predicate Class ,這些 Class 的 operator() 是預測函數,這些 Predicate Class 的 operator() 也應該是純函數,且不能修改 Class 內部成員變量 —— 換句話說,典型的 Const Member Function。 css

  1. Predicate Functions should be pure function.
  2. Predicate Class should make operator() const member function.

2 Make Functor classes adaptable

~~(╯﹏╰)b, 各類從 unary/binary_function 繼承過來的東東。。。。見下面的代碼。。。 html

3 ptr_fun, mem_fun and mem_fun_ref

這幾個函數用於生成前面提到的 unary/binary_function: java

// 20.3.7 adaptors pointers functions
/** @defgroup pointer_adaptors Adaptors for pointers to functions
 * @ingroup functors
 *
 *  The advantage of function objects over pointers to functions is that
 *  the objects in the standard library declare nested typedefs describing
 *  their argument and result types with uniform names (e.g., @c result_type
 *  from the base classes @c unary_function and @c binary_function).
 *  Sometimes those typedefs are required, not just optional.
 *
 *  Adaptors are provided to turn pointers to unary (single-argument) and
 *  binary (double-argument) functions into function objects.  The
 *  long-winded functor @c pointer_to_unary_function is constructed with a
 *  function pointer @c f, and its @c operator() called with argument @c x
 *  returns @c f(x).  The functor @c pointer_to_binary_function does the same
 *  thing, but with a double-argument @c f and @c operator().
 *
 *  The function @c ptr_fun takes a pointer-to-function @c f and constructs
 *  an instance of the appropriate functor.
 *
 *  @{
 */
/// One of the @link pointer_adaptors adaptors for function pointers@endlink.
template<typename _Arg, typename _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); }
};

/// One of the @link pointer_adaptors adaptors for function pointers@endlink.
template<typename _Arg1, typename _Arg2, typename _Result>
class pointer_to_binary_function
        : public binary_function<_Arg1, _Arg2, _Result>
{
protected:
    _Result (*_M_ptr)(_Arg1, _Arg2);

public:
    pointer_to_binary_function() { }

    explicit
    pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2))
    : _M_ptr(__x) { }

    _Result
    operator()(_Arg1 __x, _Arg2 __y) const
    { return _M_ptr(__x, __y); }
};


/// One of the @link pointer_adaptors adaptors for function pointers@endlink.
template<typename _Arg, typename _Result>
inline pointer_to_unary_function<_Arg, _Result>
ptr_fun(_Result (*__x)(_Arg))
{ return pointer_to_unary_function<_Arg, _Result>(__x); }

/// One of the @link pointer_adaptors adaptors for function pointers@endlink.
template<typename _Arg1, typename _Arg2, typename _Result>
inline pointer_to_binary_function<_Arg1, _Arg2, _Result>
ptr_fun(_Result (*__x)(_Arg1, _Arg2))
{ return pointer_to_binary_function<_Arg1, _Arg2, _Result>(__x); }


/**
 *  This is one of the @link functors functor base classes@endlink.
 */
template<typename _Arg, typename _Result>
struct unary_function
{
    /// @c argument_type is the type of the argument
    typedef _Arg    argument_type;

    /// @c result_type is the return type
    typedef _Result     result_type;
};

/**
 *  This is one of the @link functors functor base classes@endlink.
 */
template<typename _Arg1, typename _Arg2, typename _Result>
struct binary_function
{
    /// @c first_argument_type is the type of the first argument
    typedef _Arg1   first_argument_type;

    /// @c second_argument_type is the type of the second argument
    typedef _Arg2   second_argument_type;

    /// @c result_type is the return type
    typedef _Result     result_type;
};


// 20.3.8 adaptors pointers members
/** @defgroup memory_adaptors Adaptors for pointers to members
 * @ingroup functors
 *
 *  There are a total of 8 = 2^3 function objects in this family.
 *   (1) Member functions taking no arguments vs member functions taking
 *        one argument.
 *   (2) Call through pointer vs call through reference.
 *   (3) Const vs non-const member function.
 *
 *  All of this complexity is in the function objects themselves.  You can
 *   ignore it by using the helper function mem_fun and mem_fun_ref,
 *   which create whichever type of adaptor is appropriate.
 *
 *  @{
 */
/// One of the @link memory_adaptors adaptors for member
/// pointers@endlink.
template<typename _Ret, typename _Tp>
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)(); }

private:
    _Ret (_Tp::*_M_f)();
};

/// One of the @link memory_adaptors adaptors for member
/// pointers@endlink.
template<typename _Ret, typename _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 adaptor helper functions.  There are only two:
// mem_fun and mem_fun_ref.
template<typename _Ret, typename _Tp>
inline mem_fun_t<_Ret, _Tp>
mem_fun(_Ret (_Tp::*__f)())
{ return mem_fun_t<_Ret, _Tp>(__f); }

template<typename _Ret, typename _Tp>
inline mem_fun_ref_t<_Ret, _Tp>
mem_fun_ref(_Ret (_Tp::*__f)())
{ return mem_fun_ref_t<_Ret, _Tp>(__f); }

簡單來講: c++

  • ptr_fun 用於將函數指針轉換成 unay_function 或者 binary_function
  • mem_fun 用於將成員函數指針轉換成 unay_function 或者 binary_function
  • mem_fun_ref
    同 mem_fun ,不一樣之處在於 mem_fun 返回的 Functor 接受的是對象指針,而 mem_fun_ref 返回的 Functor 接受的參數爲對象引用。

在使用 STL 時候,儘可能使用上述的三個函數來生成 functor。 算法

相關文章
相關標籤/搜索