STL学习笔记(仿函数)
仿函数(Functors)
仿函数(functor),就是使一个类的使用看上去象一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了。
例如我们定义一个类:
class X{
public:
return-value operator()(arguments) const;
...
};
然后就可以把这个类别的对象当做函数调用
X fo;
...
fo(arg1,arg2) //等价于fo.operator()(arg1,arg2);
显然,这种定义形式更为复杂,却又三大妙处:
1.仿函数比一般函数更灵巧,因为它可以拥有状态。
2.每个仿函数都有其型别。因此可以将仿函数的型别当做template参数传递。
3.执行速度上,仿函数通常比函数指针更快。
仿函数可当做排序准则
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
using namespace std; class Person{
public:
string firstname() const;
string lastname() const;
...
}; class PersonSortCriterion{
public:
bool operator()(const Person&p1,const Person& p2) const {
return p1.lastname()<p2.lastname()||
(!(p2.lastname()<p1.lastname())&&
p1.firstname()<p2.firstname());
}
}; int main()
{
typedef set<Person,PersonSortCriterion> PersonSet;
PersonSet coll;
PersonSet::iterator pos;
for(pos=coll.begin();pos!=coll.end();++pos){
...
}
...
}
这里的coll适用了特殊排序准则PersonSortCritersion,而它是一个仿函数类别。所以可以当做set的template参数,而一般函数则无法做到这一点。
拥有内部状态的仿函数
下面例子展示仿函数如何模拟函数在同一时刻下拥有多个状态
#include <iostream>
#include <list>
#include <algorithm>
#include "print.cpp"
using namespace std; class IntSequence
{
private:
int value;
public:
IntSequence(int initialValue):value(initialValue){}
int operator() ()
{
return value++;
}
}; int main()
{
list<int> coll;
generate_n(back_inserter(coll),,IntSequence());
PRINT_ELEMENTS(coll);
generate(++coll.begin(),--coll.end(),IntSequence());
PRINT_ELEMENTS(coll);
}
for_each()的返回值
使用for_each()可以返回其仿函数。下面将演示这一点
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class MeanValue
{
private:
long num;
long sum;
public:
MeanValue():num(),sum(){}
void operator() (int elem)
{
num++;
sum+=elem;
}
double value()
{
return static_cast<double>(sum)/static_cast<double>(num);
}
}; int main()
{
vector<int> coll;
for(int i=;i<=;++i)
{
coll.push_back(i);
}
MeanValue mv=for_each(coll.begin(),coll.end(),MeanValue());
cout<<"mean value:"<<mv.value()<<endl;
}
预定义的仿函数
C++标准程序库提供了许多预定义的仿函数。下面列出了所有这些仿函数

对对象排序或进行比较时,一般都以less<>为预设排序准则。要使用这些仿函数,必须包含头文件<functional>。
函数配接器(Function Adapters)
所谓“函数配接器”是指能够将仿函数和另一个仿函数(或某个值,或一般函数)结合起来的仿函数。函数配接器也声明与<functional>中。
例如以下语句:
find_if(coll.begin(),coll.end(),bind2nd(greater<int>()),)
其中bind2nd是将一个二元仿函数(greater<>)转换成一元仿函数。它通常将第二参数传给“由第一参数指出”的二元仿函数,作为后者的第二参数。
下面列出了预定义的函数配接器

STL学习笔记(仿函数)的更多相关文章
- Effective STL 学习笔记 39 ~ 41
Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value
Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value */--> div.org-src-container ...
- Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据
Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...
- Effective STL 学习笔记 32 ~ 33
Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 31:排序算法
Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 Item 30: 保证目标区间足够大
Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...
- Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor
Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor */--> div ...
- Effective STL 学习笔记: Item 22 ~ 24
Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...
- Effective STL 学习笔记 Item 21:Comparison Function 相关
Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...
- Effective STL 学习笔记:19 ~ 20
Effective STL 学习笔记:19 ~ 20 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
随机推荐
- Scenes in Cocos2D
https://www.makeschool.com/docs/?source=mgwu#!/cocos2d/1.0/scenes Scenes in Cocos2D In Cocos2D you h ...
- VIM使用技巧1
.命令是vim中很重要的一个命令,用法如下: 加入有一个文件vimtest.txt,内容如下: 1 Line one 2 Line two ...
- OpenCV实践之路——人脸检测(C++/Python) 【转】
转自:http://blog.csdn.net/xingchenbingbuyu/article/details/51105159 版权声明:本文为博主原创文章,转载请联系作者取得授权. 本文由@星沉 ...
- (二十五)epoll深入理解续
转自:http://blog.csdn.net/yusiguyuan/article/details/15027821 在Linux的网络编程中,很长的时间都在使用select来做事件触发.在linu ...
- C++ 调节PCM音频音量大小
在用解码器解码音频数据得到PCM音频数据块之后,可以在将数据送给声卡播放之前调节其音量大小,具体的实现函数如下: void RaiseVolume(char* buf, UINT32 size, UI ...
- dll共享段中一些需要注意的问题
Visual C++ 如何与应用程序或其他 DLL 共享自己 DLL 中的数据? Win32 DLL 映射到调用进程的地址空间中.默认情况下,每个使用 DLL 的进程都有自己的所有 DLL 全局变量和 ...
- Delphi实现截屏存盘的方法
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- (1)WPF概述
一.wpf特点 winfrom使用GID/GID+ 图形引擎,wpf使用directx的图形引擎 通过directx提供硬件加速 类型web的布局模型 丰富的绘图模型.文本模型, 支持音频视频 可创建 ...
- HDU 2955 【01背包/小数/概率DP】
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- 牛客小白月赛3 B 躲藏【动态规划/字符串出现cwbc子序列多少次】
链接:https://www.nowcoder.com/acm/contest/87/B来源:牛客网 XHRlyb和她的小伙伴Cwbc在玩捉迷藏游戏 Cwbc藏在多个不区分大小写的字符串中. 好奇的X ...