stl源码剖析 详细学习笔记 仿函数
//---------------------------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源码剖析 详细学习笔记 仿函数的更多相关文章
- stl源码剖析 详细学习笔记 hashtable
//---------------------------15/03/24---------------------------- //hashtable { /* 概述: sgi采用的是开链法完成h ...
- stl源码剖析 详细学习笔记 set map
// // set map.cpp // 笔记 // // Created by fam on 15/3/23. // // //---------------------------15/03 ...
- stl源码剖析 详细学习笔记 算法(1)
//---------------------------15/03/27---------------------------- //算法 { /* 质变算法:会改变操作对象之值 所有的stl算法都 ...
- stl源码剖析 详细学习笔记 RB_tree (2)
//---------------------------15/03/22---------------------------- //一直好奇KeyOfValue是什么,查了下就是一个和仿函数差不多 ...
- stl源码剖析 详细学习笔记 RB_tree (1)
// // RB_tree_STL.cpp // 笔记 // // Created by fam on 15/3/21. // // #include "RB_tree_STL.h&q ...
- stl源码剖析 详细学习笔记heap
// // heap.cpp // 笔记 // // Created by fam on 15/3/15. // // //---------------------------15/03/15 ...
- stl源码剖析 详细学习笔记 空间配置器
//---------------------------15/04/05---------------------------- /* 空间配置器概述: 1:new操作包含两个阶段操作 1>调 ...
- stl源码剖析 详细学习笔记 配接器
//---------------------------15/04/03---------------------------- /* 配接器概述: 1:adapter是一种设计模式:将一个clas ...
- stl源码剖析 详细学习笔记 算法(2)
//---------------------------15/03/29---------------------------- //****************************set相 ...
随机推荐
- 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
- AD账号解锁
Get-ADUser -Filter * -Properties * -SearchBase "dc=uxin,dc=youxinpai,dc=com"| ? {$_.locke ...
- 运行结果出现Process finished with exit code 0
表示程序正常执行完毕并退出. 可以科普一下exit code,在大部分编程语言中都适用 exit code 0表示程序执行成功,正常退出 exit code 1表示执行过程中遇到了某些问题或者错误,非 ...
- 2019 wannafly winter camp day 3
2019 wannafly winter camp day 3 J 操作S等价于将S串取反,然后依次遍历取反后的串,每次加入新字符a,当前的串是T,那么这次操作之后的串就是TaT.这是第一次转化. 涉 ...
- ConstraintLayout使用手册
1. 解决痛点 主要用拖拽 解决嵌套过多 2. 简易使用手册 增加约束 四个角直接拖拽就好了 删除约束 match_constraint 属性 这个属性类似于match_parent,去掉margin ...
- Lombok快速上手(安装、使用与注解参数)
目录 Lombok插件安装与使用说明 常见参数 lombok的依赖于安装 依赖管理 IDEA插件的安装 @Data小例子 扩展@ToString 构造器注解扩展 @Log及其他日志注解 资料链接 Lo ...
- 2.Redis集群环境搭建
转载请出自出处:http://www.cnblogs.com/hd3013779515/ 一.基本概念 1.redis集群是一个可以在多个节点之间进行数据共享的设施.redis集群提供了以下两个好处1 ...
- BZOJ1941:[SDOI2010]Hide and Seek(K-D Tree)
Description 小猪iPig在PKU刚上完了无聊的猪性代数课,天资聪慧的iPig被这门对他来说无比简单的课弄得非常寂寞,为了消除寂寞感,他决定和他的好朋友giPi(鸡皮)玩一个更加寂寞的游戏- ...
- 日常使用Shell积累
HDFS统计文件行数: hdfs dfs -cat hdfs://ns3/wordcount/* |wc -l
- 【转】浅谈React、Flux 与 Redux
本文转自<浅谈React.Flux 与 Redux>,转载请注明出处. React React 是一个 View 层的框架,用来渲染视图,它主要做几件事情: 组件化 利用 props 形成 ...