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相 ...
随机推荐
- 关于 Azure Windows VM 的磁盘和 VHD
就像其他任何计算机一样,Azure 中的虚拟机将磁盘用作存储操作系统.应用程序和数据的位置. 所有 Azure 虚拟机都至少有两个磁盘,即 Windows 操作系统磁盘和临时磁盘. 操作系统磁盘基于映 ...
- JavaScript、ES5、ES6的区别
一.什么是JavaScript 1.JavaScript一种动态类型.弱类型.基于原型的客户端脚本语言,用来给HTML网页增加动态功能. 动态:在运行时确定数据类型.变量使用之前不需要类型声明,通常变 ...
- cdn刷新和对应的浏览器现象
1.浏览器手动点刷新,会发起网络请求,从cdn判断last-modify时间是否一致,未过期则返回304,如果已经过期则返回200,重新请求 关键在于发起的网络请求中'If-Modified-Sinc ...
- npm安装vue
目录 npm安装vue Vue.js 是什么 直接用script引入 安装vue 对不同构建版本的解释 安装命令行工具 (CLI) 安装cnpm 安装vue-cli 新建vue项目 运行服务 目录结构 ...
- 第 15 章 位操作(invert4)
/*------------------------------------ invert4.c -- 使用位操作显示二进制 ------------------------------------* ...
- Javaweb学习(二):Http通信协议
当我们开始jsp/servlet编程之旅之前,我们还需要知道一些关于网络通讯方面的一些知识.这样能更加有助于我们的理解,希望大家能看懂我的描述,而不至于在学习的路上一知半解.(手动比❤) 认识Ht ...
- 微软撤回sharepoint 2013 sp1
微软撤回sharepoint 2013 sp1, 现在已经不能下载32bits和64bits. 以下是我们发现的问题(未必一定和SP1有关) - Search SSA managed metadata ...
- [BZOJ 3167][HEOI 2013]SAO
[BZOJ 3167][HEOI 2013]SAO 题意 对一个长度为 \(n\) 的排列作出 \(n-1\) 种限制, 每种限制形如 "\(x\) 在 \(y\) 之前" 或 & ...
- Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128)
在 linux服务器上运行代码报错: Python3中遇到UnicodeEncodeError: ‘ascii’ codec can’t encode characters in ordinal no ...
- Oracle Spatial中SDO_Geometry说明
Oracle Spatial中SDO_Geometry说明 在ArcGIS中通过SDE存储空间数据到Oracle中有多种存储方式,分别有:二进制Long Raw .ESRI的ST_Geometry以及 ...