Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2570215
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: C/C++

2013-05-05 15:16:01

ptr_fun,mem_fun,mem_fun_ref 浅析

 一 ptr_fun:
  1. list widgetPtrs;  
  2. bool isInteresting(const Widget *pw);  
  3. list::iterator i = find_if(widgetPtrs.begin(), widgetPtrs.end(), not1(isInteresting));   // 错误!不能编译  
  4. list::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,

作者:【lgp88】 http://blog.csdn.net/lgp88/article/details/7058328

阅读(884) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~