7 仿函数
来源:互联网 发布:qq游戏端口 编辑:程序博客网 时间:2024/06/05 20:46
7.1 仿函数概念
使用方法:
greater<int> ig;cout << ig(4,6); //常规用法cout << greater<int>()(6,4); //使用临时对象。
7.2 可配接的关键
为了拥有可配接能力,每一个仿函数必须定义自己的相应型别。为了方便,定义了两个class,分别代表一元仿函数和二元仿函数(STL不支持三元仿函数)。任何仿函数,只有继承其中一个class,就可以获得相应型别,也就自动拥有了配接能力。
1、unary_function——用于一元函数
template <class Arg, class Result>struct unary_function {typefef Arg argument_type;typedef Result result_type;}2、binary_function——用于二元函数
template <class Arg1, class Arg2, class Result>struct binary_function{typedef Arg1 first_argument_type;typedef Arg2 secong_argument_type;typedef Result result_type;}
7.3 算数类仿函数
plus<T> minus<T> multiplies<T> divides<T> modules<T> negate<T>
template <class T>struct plus : public binary_function<T, T, T> {T operator()(const T& x, const T& y) const{result x + y;}};
7.4 关系运算符
equal_to<T> not_equal_to<T> greater<T> greater_equal<T> less<T> less_equal<T>
template <class T>struct equal_to : public binary_function<T, T, bool> {T operator()(const T& x, const T& y) const{result x == y;}};
7.5 逻辑运算符
logical_and<T> logical_or<T> logical_not<T>
7.6 证同(identity)、选择(select)、投射(project)
这些仿函数只是将参数原封不动的传回,这只是为了间接性,间接性是抽象化的重要工具。
template <class T>struct idendity : public unary_function<T, T>{const T& operator()(const T& x) const { return x; }};
template <class Pair>struct select1st : public unary_function<Pair, typename Pair::first_type> {const typename Pair::first_type& operater()(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& operater()(const Pair& x) const { return x.second; }};
0 0
- 7 仿函数
- 仿函数
- 仿函数
- 仿函数
- 仿函数
- 仿函数
- 仿函数
- 仿函数
- 仿函数
- 仿函数
- 仿函数
- 仿函数
- 仿函数
- 仿函数
- 仿函数
- 仿函数
- 仿函数
- 仿函数
- WEB测试流程
- 传智播客网页平面UI精品就业班、UI周末提高班学费调整公告
- HTC Desire Z刷recovery刷机教程
- C++代码风格04
- 守护进程脚本
- 7 仿函数
- Fiddler 教程
- Storm编程概述之我见
- liunx中cat less more的区别
- HTC G21刷机具体教程 通过recovery程序刷入
- select函数
- centos 下面mount window下面的share 目录
- PCI9054
- 货架控件根据选择的区域来决定是否格式化显示