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相 ...
随机推荐
- COCOMOII
一.COCOMOII是什么 cocomo是 COnstructive COst MOdel(建设性成本估算模型)的缩写.最早是由Dr. Barry Boehm在1981年提出.是一种精确的.易于使用的 ...
- 转:总结const、readonly、static三者的区别
const:静态常量,也称编译时常量(compile-time constants),属于类型级,通过类名直接访问,被所有对象共享! a.叫编译时常量的原因是它编译时会将其替换为所对应的值: b.静态 ...
- webservice安全性浅谈
原文地址:http://www.cnblogs.com/chhuic/archive/2009/11/19/1606109.html 做项目时,经常会用到WebService来通讯,但WebServi ...
- centos 7 linux x64
1.修改软件源 sudo wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum updat ...
- Git钩子详解
钩子 Git钩子是在Git仓库中特定事件发生时自动运行的脚本.可以定制一些钩子,这些钩子可以在特定的情况下被执行,分为Client端的钩子和Server端的钩子.Client端钩子被operation ...
- Jar版本:java.lang.UnsupportedClassVersionError: ******
错误原因编译Java和运行Java所使用的Java的版本不一致导致:解决办法修改运行环境的Java版本或者修改编译环境的Java版本,让两者保持一致即可: java.lang.UnsupportedC ...
- 开源作业调度框架 - Quartz.NET - 实战使用2
纠正第一篇文章的一个错误代码. 下面是错误代码,这样并不能得知系统中是否已经存在该JobId //实例化一个作业Key对象,用于获取作业对象或判断是否存在作业时使用. JobKey jobKey = ...
- phpstorm 的.idea 目录加入.gitignore无效的解决方法
无效的原因是:对应的目录或者文件已经被git跟踪,此时再加入.gitignore后就无效了, 解决办法: 先执行 git rm -r --cached .idea 再重新加入.gitignore文件 ...
- Math.min() / Math.max() 使用方法
首先弄懂apply 和 call 都是js函数自带的方法.区别如下: apply和call的用法只有一个地方不一样,除此之外,其他地方基本一模一样 1. a.call(b,arg1,arg2…) 2. ...
- node.js cheerio API
安装 npm install cheerio load var cheerio = require('cheerio'), $ = cheerio.load('<ul id=“fruits”&g ...