//---------------------------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. Oracle EBS INV 获取现有量等值

    DECLARE L_api_return_status VARCHAR2(1); l_qty_oh NUMBER; l_qty_res_oh NUMBER; l_qty_res NUMBER; l_q ...

  2. Oracle EBS AR 贷项通知单核销取值

    SELECT cm.trx_number ,fnd_flex_ext.get_segs('SQLGL', 'GL#', gcc.chart_of_accounts_id, ad.code_combin ...

  3. NodeJS中MySql的增删改查操作

    纯粹记录一下最基础写法,几乎没有写什么逻辑,写法也并不是很完善(因为我自己也刚刚摸索出来这么写可以...= =!)    望高手指教   也希望能够帮到比我还新的新手.... //1.insert操作 ...

  4. 中式台球 规则 ( ChinaBilliards )

    中式台球比赛规则 中式台球兴起于上世纪八十年代末,之前叫法有“中式8球”.“中式9球”.“十六彩”.“美式落袋”.“普尔“.”八球””等等.中国台球协会于2012年宣布统一该项运动称呼,定名为“中式台 ...

  5. alias 别名

    别名的作用: 1.通过给危险命令加一些保护参数,防止人为误操作. 2.把很多复杂的字符串或命令变成一个简单的字符串或命令. alias 用法: 定义别名: alias rm='echo "没 ...

  6. MySQL报错:error1130

    ERROR (HY000): Host 'ip-172-31-x-x.ec2.internal' is not allowed to connect to this MySQL server 分析,从 ...

  7. mitmproxy

    通过脚本定制化实现篡改request或者response mitmproxy 顾名思义中间人代理[man-in-the-middle proxy],和fiddler.Charles等工具类似,通过代理 ...

  8. 【Alpha 冲刺】 11/12

    今日任务总结 人员 今日原定任务 完成情况 遇到问题 贡献值 胡武成 完成app端api编写 未完成 文件上传api还没完成 孙浩楷 1. 与后端交接, 2. 完成图片在线编辑插件引入 未完成 陷入僵 ...

  9. 团队作业7——第二次项目冲刺(Beta版本)day1

    项目成员:  曾海明(组长):201421122036 于波(组员):201421122058 蓝朝浩(组员):201421122048 王珏 (组员):201421122057 叶赐红(组员):20 ...

  10. MySql详解(五)

    MySql详解(五) MySql库的管理 一.创建库 create database [if not exists] 库名[ character set 字符集名]; 二.修改库 alter data ...