Bind2nd源码解析
来源:互联网 发布:伪造短信软件 编辑:程序博客网 时间:2024/05/22 17:32
例:transform(coll1.begin(), coll1.end(), back_inserter(coll2), bind2nd(multiplies<int>(), 10));
1、调用模板函数bind2nd,第一个参数为multiplies<int>临时对象。
// TEMPLATE FUNCTION bind2ndtemplate<class _Fn2,class _Ty> inlinebinder2nd<_Fn2> bind2nd(const _Fn2& _Func, const _Ty& _Right){// return a binder2nd functor adaptertypename _Fn2::second_argument_type _Val(_Right);//此处将仿函数multiplies<int>临时对象的第二个参数设置为10return (_STD binder2nd<_Fn2>(_Func, _Val)); //返回模板类binder2nd临时对象}
2、模板类binder2nd的定义:
// TEMPLATE CLASS binder2ndtemplate<class _Fn2>class binder2nd: public unary_function<typename _Fn2::first_argument_type,typename _Fn2::result_type> //设置参数类型和返回值类型{// functor adapter _Func(left, stored)public:typedef unary_function<typename _Fn2::first_argument_type,typename _Fn2::result_type> _Base;typedef typename _Base::argument_type argument_type;typedef typename _Base::result_type result_type;binder2nd(const _Fn2& _Func,const typename _Fn2::second_argument_type& _Right) //构造函数,仿函数第二个参数被赋值: op(_Func), value(_Right){// construct from functor and right operand}result_type operator()(const argument_type& _Left) const //调用仿函数{// apply functor to operandsreturn (op(_Left, value));}result_type operator()(argument_type& _Left) const{// apply functor to operandsreturn (op(_Left, value));}protected:_Fn2 op;// the functor to applytypename _Fn2::second_argument_type value;// the right operand};
3、模板类binder2nd模板基类unary_function的定义:
// FUNCTIONAL STUFF (from <functional>)// TEMPLATE STRUCT unary_functiontemplate<class _Arg,class _Result>struct unary_function{// base class for unary functionstypedef _Arg argument_type;typedef _Result result_type;};
Bind2nd模板函数返回一个Binder2nd临时对象,该对象也是个函数对象,在算法transform调用Binder2nd仿函数时,Binder2nd调用第一个参数指定的函数对象。
算法transform的源码:
// TEMPLATE FUNCTION transform WITH UNARY OP //只是其中一个定义template<class _InIt, class _OutIt, class _Fn1> inline _OutIt _Transform(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn1 _Func) { // transform [_First, _Last) with _Func for (; _First != _Last; ++_First, ++_Dest) *_Dest = _Func(*_First); //调用Binder2nd函数对象,将第一个参数传递进去,第二个参数在生成临时对象时赋值. return (_Dest); }
0 0
- Bind2nd源码解析
- bind1st 和 bind2nd 解析
- bind1st与bind2nd函数解析
- bind1st与bind2nd函数解析
- bind2nd
- 解析bind1st和bind2nd的使用
- 解析bind1st和bind2nd的使用
- 【STL】bind1st与bind2nd函数解析
- (转)解析bind1st和bind2nd的使用
- 解析bind1st和bind2nd的使用
- STL中bind1st和bind2nd之解析
- 【STL】bind1st与bind2nd函数解析
- (转)解析bind1st和bind2nd的使用
- STL约束器之bind2nd(x, y)实例解析
- STL约束器之bind2nd(x, y)实例解析
- bind2nd使用
- bind1st & bind2nd
- bind2nd msdn
- Dirichlet's Theorem on Arithmetic Progressions
- Java编程思想笔记——第十六章
- 南桥和北桥芯片的概念(百度转)
- 免费WiFi还可发展X年?
- LabWindows/CVI软件下载(CVI2009,CVI2013)
- Bind2nd源码解析
- 桌面的文件突然不见了怎么恢复
- [LeetCode] Candy
- 【机器学习算法-python实现】采样算法的简单实现
- Java 打开可运行jar包(干货)
- @ Replica set 多服务器 高可用 配置 (添加删除节点方法)
- 数据的检索-count(字段名)与count(*)区别
- AIX上安装Oracle10G软件
- Mini6410基于Linux2.6.36内核制作initramfs文件系统