Effective STL 学习笔记 39 ~ 41

*/-->

div.org-src-container {
font-size: 85%;
font-family: monospace;
}
pre.src {
background-color:#2e3436;
color:#fefffe;
}

p {font-size: 15px}
li {font-size: 15px}

1 Make Predicate pure Function

纯函数 (Pure Function) 是指输出仅在输入变化时才发生变化的的函数,换句话说,该类型函数的输出不依赖于输入之外的任何东西,例如自身状态或者全局变量。这也是 Functional Programming 中的一个重要概念。 C++ 中用于 STL 算法的
Functor 是一些 Predicate Class ,这些 Class 的 operator() 是预测函数,这些 Predicate Class
的 operator() 也应该是纯函数,且不能修改 Class 内部成员变量 —— 换句话说,典型的 Const Member
Function。

  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 继承过来的东东。。。。见下面的代码。。。

3 ptr_fun, mem_fun and mem_fun_ref

这几个函数用于生成前面提到的 unary/binary_function:

// 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); }

简单来说:

  • 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。

Effective STL 学习笔记 39 ~ 41的更多相关文章

  1. Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value

    Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value */--> div.org-src-container ...

  2. Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据

    Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...

  3. Effective STL 学习笔记 32 ~ 33

    Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  4. Effective STL 学习笔记 31:排序算法

    Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  5. Effective STL 学习笔记 Item 30: 保证目标区间足够大

    Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...

  6. Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor

    Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor */--> div ...

  7. Effective STL 学习笔记: Item 22 ~ 24

    Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...

  8. Effective STL 学习笔记 Item 21:Comparison Function 相关

    Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...

  9. Effective STL 学习笔记:19 ~ 20

    Effective STL 学习笔记:19 ~ 20 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

随机推荐

  1. R语言的ARIMA模型预测

    R通过RODBC连接数据库 stats包中的st函数建立时间序列 funitRoot包中的unitrootTest函数检验单位根 forecast包中的函数进行预测 差分用timeSeries包中di ...

  2. 读论文Machine Learning for Improved Diagnosis and Prognosis in Healthcare

    Deep Learning的基本思想 假设我们有一个系统S,它有n层(S1,…Sn),它的输入是I,输出是O,形象地表示为: I =>S1=>S2=>…..=>Sn => ...

  3. label和fieldset标签

    一.label标签 作用:可以通过for属性关联input标签的 id 属性,这样可以实现在点击label标签的内容时,可以使input文本框中获取输入的光标. <body> <la ...

  4. Java基础-面向对象第三大特性之多态(polymorphism )

    Java基础-面向对象第三大特性之多态(polymorphism) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.多态概述 多态是继封装,继承之后,面向对象的第三大特性,多态的 ...

  5. MAC下 Apache服务器配置

    今天做了一个注册登录提交的页面,后续操作需要用到后端的知识 php+Mysql,之前只是有些了解,现在开始具体操作了,首先从配置环境开始.查了好几篇文档与博客,了解了挺多知识. Mac下Apache服 ...

  6. Docker中执行Shell出现乱码

    问题描述 最近遇到一个问题: 执行命令 docker exec f4af9b sh -c 'bash /tmp/build.sh' 命令在docker中执行shell,会出现中文乱码的问题.但是在do ...

  7. 奇葩字符 "a๎๎๎๎๎๎๎๎๎๎๎๎๎๎๎๎๎๎๎" 的简单分析

    这个其实之前火过一阵子,当时也没怎么注意,今天看到空间里又有人在刷这个字符了,所以决定分析下他是什么东西.复制这个字符在控制台查看 "a๎๎๎๎๎๎๎๎๎๎๎๎๎๎๎๎๎๎๎".l ...

  8. asp.net(C#)中 DataTime 赋空值的研究

    SqlServer中的datetime类型的空值和c#中的DateTime的空值的研究 在SqlServer 2000中datetime 的空值即默认值为1900-01-01 00:00:00,C#中 ...

  9. 精心整理的十个必须要知道CSS+DIV技巧

    1.css font的简写规则  当我们写字体样式的时候,我们也许会这样子写 font-size: 1em; line-height: 1.5em; font-weight: bold; font-s ...

  10. 【Linux】VirtualBox安装ubuntu排错LowGraphic

    在Oracle的VirtualBox虚拟机上,安装Ubuntu后,提示了如下图这样的 错误 The system is running in low-graphics mode 在网上搜,有多种解答方 ...