【STL】ptr_fun详解
来源:互联网 发布:justin bieber 知乎 编辑:程序博客网 时间:2024/06/05 04:35
ptr_fun是将一个普通的函数适配成一个仿函数(functor), 添加上argument_type和result type等类型,它的定义如下:
template<class _Arg1, class _Arg2, class _Result> inline pointer_to_binary_function<_Arg1, _Arg2, _Result, _Result(__clrcall *)(_Arg1, _Arg2)> ptr_fun(_Result (__clrcall *_Left)(_Arg1, _Arg2)) { // return pointer_to_binary_function functor adapter return (pointer_to_binary_function<_Arg1, _Arg2, _Result, _Result (__clrcall *)(_Arg1, _Arg2)>(_Left)); }下面的例子就是说明了使用ptr_fun将普通函数(两个参数, 如果有多个参数, 要改用boost::bind)适配成bind1st或bind2nd能够使用的functor,否则对bind1st或bind2nd直接绑定普通函数,则编译出错。
#include <algorithm> #include <functional> #include <iostream> using namespace std; int sum(int arg1, int arg2) { std::cout<< "arg1 = " << arg1 << std::endl; std::cout<< "arg2 = " << arg2 << std::endl; int sum = arg1 + arg2; std::cout << "sum = " << sum << std::endl; return sum; }int main(int argc, char *argv[], char *env[]){ bind1st(ptr_fun(sum), 1)(2);// the same as sum(1,2) bind2nd(ptr_fun(sum), 1)(2);// the same as sum(2,1) return 0;}
- 【STL】ptr_fun详解
- ptr_fun详解
- ptr_fun详解
- STL中的ptr_fun
- ptr_fun
- ptr_fun
- ptr_fun
- stl中functional中的ptr_fun和pointer_to_binary_function
- STL示例08(ptr_fun和random_shuffle搭配使用)
- 了解使用ptr_fun、mem_fun和mem_fun_ref的原因1(Effective stl 条款41)
- 了解使用ptr_fun、mem_fun和mem_fun_ref的原因2(Effective stl 条款41)
- 了解使用ptr_fun、mem_fun和mem_fun_ref的原因3(Effective stl 条款41)
- effective stl 第41条:理解ptr_fun/mem_fun/mem_fun_ref的由来
- Effective STL 41 Understand the reasons for ptr_fun, mem_fun, and mem_fun_ref
- STL 详解
- STL详解
- STL详解
- STL详解
- IT知识体系结构图
- 构造函数与析构函数(C++)
- shell命令执行控制
- ibatis源码学习1_整体设计和核心流程
- 派生与私有保护
- 【STL】ptr_fun详解
- 百科知识:HBA卡
- 实时多线程系统的日志实现
- MyEclipse里有些特殊符号,不知道还以为是乱码
- MAC版“迅雷 意外退出”
- 正则验证加js大全,真的很给力!!!很全啊有木有
- memcached的安装与应用
- ExtJs表单验证
- 深入了解计算机系统 家庭作业 2.66