//---------------------------15/04/01----------------------------

//仿函数是为了算法而诞生的,可以作为算法的一个参数,来自定义各种操作,比如比大小,返回bool值,对元素进行操作等

//虽然这些函数也能实现,但是如果配合配接器(adapter)可以产生更灵活的变化。

//为了使对象像函数一样,就必须重载operator()

//unary_function

template<class Arg,
class Result>

struct unary_function

{

//参数类型

typedef Arg argument_type;

//返回值类型

typedef Result result_type;

};

//binary_functione

//二元仿函数

template<class Arg1,
class Arg2, class Result>

struct binary_functione

{

typedef Arg1 first_argument_type;

typedef arg2 second_argument_type;

typedef Result result_type;

};

//算术类仿函数

template<class T>

struct plus :
public binary_functione<T, T, T>

{

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

{

return x + y;

}

};

template<class T>

struct minus :
public binary_functione<T, T, T>

{

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

{

return x - y;

}

};

template<class T>

struct multiplies :
public binary_functione<T, T, T>

{

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

{

return x * y;

}

};

template<class T>

struct divides :
public binary_functione<T, T, T>

{

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

{

return x / y;

}

};

template<class T>

struct modulus :
public binary_functione<T, T, T>

{

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

{

return x & y;

}

};

template<class T>

struct negate:
public unary_function<T, T>

{

T operator()(const T& x)
const

{

return -x;

}

};

//证同元素,数值a与该元素做op操作会得到自己。
加法的证同元素为0 乘法为1

template<class T>

inline T identity_element(plus<T>)

{

);

};

template<class T>

inline T identity_element(multiplies<T>)

{

);

};

//关系运算类仿函数

template<class T>

struct equal_to :
public binary_functione<T, T,
bool>

{

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

{

return x == y;

}

};

template<class T>

struct not_equal_to :
public binary_functione<T, T,
bool>

{

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

{

return x != y;

}

};

template<class T>

struct greater :
public binary_functione<T, T,
bool>

{

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

{

return x > y;

}

};

template<class T>

struct less :
public binary_functione<T, T,
bool>

{

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

{

return x < y;

}

};

template<class T>

struct greater_equal :
public binary_functione<T, T,
bool>

{

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

{

return x >= y;

}

};

template<class T>

struct less_equal :
public binary_functione<T, T,
bool>

{

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

{

return x <= y;

}

};

//逻辑类仿函数

template<class T>

struct logical_and :
public binary_functione<T, T,
bool>

{

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

{

return x && y;

}

};

template<class T>

struct logical_or :
public binary_functione<T, T,
bool>

{

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

{

return x || y;

}

};

template<class T>

struct logical_not :
public unary_function<T,
bool>

{

bool operator()(const T& x)
const

{

return !x;

}

};

//证同函数,任何数通过此函数调用运算后返回原值。

template<class T>

struct identity :
public unary_function<T, T>

{

const T&
operator()(const T& x)
const

{

return x;

}

};

//选择函数
接受pair,传回第一个元素

template<class Pair>

struct select1st :
public unary_function<Pair,
typename 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,
typename Pair::second_type>

{

const typename Pair::second_type&
operator()(const Pair& x)
const

{

return x.second;

}

};

//投射函数:传回第一参数,忽略第二参数

template<class Arg1,
class Arg2>

struct project1st :
public binary_functione<Arg1, Arg2, Arg1>

{

Arg1
operator()(const Arg1& x,
const Arg2& y) const

{

return x;

}

}

template<class Arg1,
class Arg2>

struct project2nd :
public binary_functione<Arg1, Arg2, Arg2>

{

Arg2
operator()(const Arg1& x,
const Arg2& y) const

{

return y;

}

}

stl源码剖析 详细学习笔记 仿函数的更多相关文章

  1. stl源码剖析 详细学习笔记 hashtable

    //---------------------------15/03/24---------------------------- //hashtable { /* 概述: sgi采用的是开链法完成h ...

  2. stl源码剖析 详细学习笔记 set map

    // //  set map.cpp //  笔记 // //  Created by fam on 15/3/23. // // //---------------------------15/03 ...

  3. stl源码剖析 详细学习笔记 算法(1)

    //---------------------------15/03/27---------------------------- //算法 { /* 质变算法:会改变操作对象之值 所有的stl算法都 ...

  4. stl源码剖析 详细学习笔记 RB_tree (2)

    //---------------------------15/03/22---------------------------- //一直好奇KeyOfValue是什么,查了下就是一个和仿函数差不多 ...

  5. stl源码剖析 详细学习笔记 RB_tree (1)

    // //  RB_tree_STL.cpp //  笔记 // //  Created by fam on 15/3/21. // // #include "RB_tree_STL.h&q ...

  6. stl源码剖析 详细学习笔记heap

    // //  heap.cpp //  笔记 // //  Created by fam on 15/3/15. // // //---------------------------15/03/15 ...

  7. stl源码剖析 详细学习笔记 空间配置器

    //---------------------------15/04/05---------------------------- /* 空间配置器概述: 1:new操作包含两个阶段操作 1>调 ...

  8. stl源码剖析 详细学习笔记 配接器

    //---------------------------15/04/03---------------------------- /* 配接器概述: 1:adapter是一种设计模式:将一个clas ...

  9. stl源码剖析 详细学习笔记 算法(2)

    //---------------------------15/03/29---------------------------- //****************************set相 ...

随机推荐

  1. 使用python做简单的接口性能测试

    思路:利用ruquest发送请求,利用多线程模拟并发 下面直接上代码: #!/user/bin/env python #coding=utf-8 import requests import date ...

  2. memcached编译安装报错 ,提示checking build system type... Invalid configuration `x86_64-unknown-linux-': machine `x86_64-unknown-linux' not recognized configure: error: /bin/sh ./config.sub x86_64-unknown-linu

  3. 关于Apache连接数限制的设置

    昨天晚上收到监视团队的电话,说web服务器连不上.(作为DBA,这貌似超出了我的工作范畴啊...) 于是马上VPN连上服务器,发现网络负载均衡下的两台Apache服务器都没有响应,而服务器OS层面上正 ...

  4. cat > file << EOF 与 cat > file << -

    当我们在使用kickstart 的时候,会遇到写网卡配置文件的情况,这时候我们使用cat > file << EOF 命令等,可以从标准输入中接受输入并保存到 file 文件中. c ...

  5. Linux运维之系统性能---vmstat工具分析内存的瓶颈

    为了提高磁盘存取效率, Linux做了一些精心的设计, 除了对dentry进行缓存(用于VFS,加速文件路径名到inode的转换), 还采取了两种主要Cache方式:Buffer Cache和Page ...

  6. November 11th, 2017 Week 45th Saturday

    Happiness is a direction, not a place. 快乐是一个方向,不是一个目的. Do you remember those moments in your life wh ...

  7. html简单介绍(一)

    什么是html HTML 是用来描述网页的一种语言.HTML 指的是超文本标记语言 (Hyper Text Markup Language)HTML 不是一种编程语言,而是一种标记语言 (markup ...

  8. 谁对EXTJS熟悉,有关关闭panel的问题?

    谁对EXTJS熟悉,有关关闭panel的问题?比如:我在A.js 中写了一个  var  win  =   new  Ext.Window( { b.js });   win.show(); 打开了一 ...

  9. JAVA反射机制_获取字节码文件对象

    是在运行状态中,对于任意一个类 (class文件),都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性: 这种动态获取的信息以及动态调用对象的方法的功能称为java语 ...

  10. Algorithms: 二叉平衡树(AVL)

    二叉平衡树(AVL):   这个数据结构我在三月份学数据结构结构的时候遇到过.但当时没调通.也就没写下来.前几天要用的时候给调好了!详细AVL是什么,我就不介绍了,维基百科都有.  后面两月又要忙了. ...