ptr_fun,mem_fun,mem_fun_ref 浅析

 一 ptr_fun:
list<Widget*> widgetPtrs;
bool isInteresting(const Widget *pw);
list<Widget*>::iterator i = find_if(widgetPtrs.begin(), widgetPtrs.end(), not1(isInteresting));   // 错误!不能编译
list<Widget*>::iterator i = find_if(widgetPtrs.begin(), widgetPtrs.end(), not1(ptr_func(isInteresting)));  // 没问题
这几句代码引出了一些问题。为什么必须在应用not1前对isInteresting应用ptr_fun?ptr_fun为我们做了什么,怎么完成上面的工作的?
答案多少有些令人惊讶:
ptr_fun做的唯一的事是使一些typedef有效(  仿函数类的operator()所带的参数的类型和它的返回类型。对于binary_function,你要指定三个类型:你的operator的第一个和第二个参数的类型,和你的operator地返回类型; 而这两个基类 typedef 了argument_type、first_argument_type、second_argument_type和result_type 这几种类型 )。就是这样。not1需要这些typedef,这就是为什么可以把not1应用于ptr_fun,但不能直接对isInteresting应用not1。因为是低级的函数指针,isInteresting缺乏not1需要的typedef。

not1不是STL中唯一有那些要求的组件。四个标准函数适配器(not1、not2、bind1st和bind2nd)都需要存在某些typedef

二 mem_fun,mem_fun_ref

1.包含如上作用,提供重要的typedef

2.只要传一个成员函数给STL组件,就必须使用mem_fun,mem_fun_ref,

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值