ptr_fun学习笔记
来源:互联网 发布:平板上的淘宝怎么开店 编辑:程序博客网 时间:2024/06/01 10:05
ptr_fun是将一个普通的函数适配成一个functor,添加上argument type和result type等类型, 其实现如下(例子里面是binary_function,unary_function同理):
由上面的代码可见,ptr_fun只是将一个普通的函数(或者函数指针)适配成类pointer_to_binary_function,而该类实际上是binary_function的子类,这样出来的functor就有利于同STL的算法等适配。
下面的例子就是说明了使用ptr_fun将普通的函数适配成bind1st或bind2nd能够使用的functor,否则对bind1st或bind2nd直接绑定普通函数,则编译出错。
- template<class _Arg1,
- class _Arg2,
- class _Result> inline
- pointer_to_binary_function<_Arg1, _Arg2, _Result,
- _Result(__fastcall *)(_Arg1, _Arg2)>
- ptr_fun(_Result (__fastcall *_Left)(_Arg1, _Arg2))
- { // return pointer_to_binary_function functor adapter
- return (std::pointer_to_binary_function<_Arg1, _Arg2, _Result,
- _Result (__fastcall *)(_Arg1, _Arg2)>(_Left));
- }
由上面的代码可见,ptr_fun只是将一个普通的函数(或者函数指针)适配成类pointer_to_binary_function,而该类实际上是binary_function的子类,这样出来的functor就有利于同STL的算法等适配。
下面的例子就是说明了使用ptr_fun将普通的函数适配成bind1st或bind2nd能够使用的functor,否则对bind1st或bind2nd直接绑定普通函数,则编译出错。
- #include <algorithm>
- #include <functional>
- #include <iostream>
- using namespace std;
- int sum(int arg1, int arg2)
- {
- cout<<"ARG 1: "<<arg1<<endl;
- cout<<"ARG 2: "<<arg2<<endl;
- int sum = arg1 + arg2;
- cout<<"SUM: "<<sum<<endl;
- return sum;
- }
- int main(int argc,char* argv[])
- {
- bind1st(ptr_fun(sum),1)(2); // the same as sum(1,2)
- bind2nd(ptr_fun(sum),1)(2); //the same as sum(2,1)
- getchar();
- return 0;
- }
0 0
- ptr_fun学习笔记
- ptr_fun
- ptr_fun
- ptr_fun
- ptr_fun详解
- ptr_fun详解
- ptr_fun,mem_fun,mem_fun_ref 浅析
- 【STL】ptr_fun详解
- STL中的ptr_fun
- mem_fun_t/ptr_fun等用法
- C++标准库---ptr_fun()
- ptr_fun mem_fun_ref mem_fun bind1st bind2nd
- 配接器ptr_fun的一点增补
- mem_fun_ref,mem_fun,not1,not2,ptr_fun
- mem_fun_ref,mem_fun,not1,not2,ptr_fun
- ptr_fun,mem_fun,mem_fun_ref的使用
- 使用ptr_fun、mem_fun和mem_fun_ref
- ptr_fun,mem_fun仿函数类适配器
- ac 自动机
- Hibernate学习_004_Hibernate中常用的主键生成策略总结(下)Annotation方式
- uva 131(枚举子集)
- nyist oj 19 擅长排列的小明(dfs搜索+STL)
- Service
- ptr_fun学习笔记
- Ubuntu14.04LTS x64 Eclipse图标为问号及深度音乐卸载后声音状态栏仍显示的解决方法
- android 开发UI好工具-----dump View Hierarchy for UI automator
- HDOJ题目2122Ice_cream’s world III(最小生成树)
- codeblocks的cppcheck设置
- Android 智能问答机器人的实现
- OJ 2135用的数组
- 王家林最受欢迎的一站式云计算大数据和移动互联网解决方案课程 V1(20140809)之Hadoop企业级完整训练:Rocky的16堂课(HDFS&MapReduce&HBase&Hive&Zookee
- 一个Codility上的练习遇到和预期结果不同的问题。