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; ...
随机推荐
- 孙鑫 VC++深入详解第14课——TCP通信/UDP通信(转)
原文转自 http://blog.csdn.net/supersonico/article/details/18900319 一.用VC++来编写TCP 服务器/客户端的简单程序. 注意事项: 1.要 ...
- UVA 11925:Generating Permutations(冒泡排序 Grade D)
VJ题目链接 题意:n个数(n<300),是一个1~n的某个排列.有两种操作:操作1把前两个数换位置,操作2把第一个数移动到最后.问给出一个排列,问通过怎样的操作,能把1,2,3,...,n变到 ...
- os.path.isfile的错误
今天写了一个程序,读取子目录(source)下的所有文件,然后转换. 程序一部分代码如下: def DtoTab(dsrc, dtarget): try: for item in os.listdir ...
- Codeforces Round #267 (Div. 2) B. Fedor and New Game【位运算/给你m+1个数让你判断所给数的二进制形式与第m+1个数不相同的位数是不是小于等于k,是的话就累计起来】
After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play ...
- Map泛型集合-显示企鹅信息
package collection; /** * 宠物类 * @author * */ public class Pet { private String name; private String ...
- unity3d Billboard
CameraFacingBillboard CameraFacingBillboard From Unify Community Wiki Jump to: navigation, searc ...
- steelray project viewer
steelray project viewer是一款英文语言软件,透过Steelray Project Viewer,可以打开.导航.浏览.打印Microsoft Project的.mpp文件.
- 各种计算机编码与base64
什么是base64,base64与Hex编码,ASCII编码,UTF-8编码都是什么关系 1 计算机开始之初,二进制 计算机所用的语言是什么呢?这个语言非常简单,只有0和1两种表示.0代表否,1代表是 ...
- Chrome下强制http重定向到https的问题
问题: Chrome会强制将http重定向到https,就算是在浏览器手动输入http://xxx也不可以. 解决方案: 1. 在chrome的地址栏输入chrome://net-internals/ ...
- sql server 2008 R2 数据库文件大小限制
select @@version SQL2005 分五个版本,如下所列: 1.Enterprise(企业版) 2.Development(开发版) 3.Workgroup,(工作群版) 4.Stand ...