仿函数

STL中的函数配接器,是指能够将仿函数和另一个仿函数或某个值或某一个一般函数结合起来形成一个新的仿函数。

在STL中函数配接器一共有四个,分别是:

bind1nd(op ,value) 相当于构成op(value,param),即把value结合成op的第一个参数

bind2nd(op ,value)相当于构成op(param,value),即把value结合成op的第二个参数

not1(op)相当于构成!op(param),即op(param)的结果进行逻辑非运算

not2(op)相当于构成!op(param,param),即把op(param,param)的结构进行逻辑非运算

下面先以bind2nd为例示例:

[cpp]  view plain  copy
  1. #include<iostream>  
  2. #include<vector>  
  3. #include<algorithm>  
  4. #include<iterator>  
  5. #include<functional>  
  6. using namespace std;  
  7.   
  8. class A :public binary_function<int,int,void>  
  9. {  
  10. public:  
  11.     A(){}  
  12.     ~A(){}  
  13.     void operator()( int a, int b) const  
  14.     {  
  15.         ostream_iterator<int> os(cout,"--");  
  16.         *os = a + b;  
  17.     }  
  18. private:  
  19. };  
  20.   
  21. void fun(int a,int b)  
  22. {  
  23.     ostream_iterator<int> os(cout,"--");  
  24.     *os = a + b;  
  25. }  
  26.   
  27. int Get(int a)  
  28. {  
  29.     return a;  
  30. }  
  31. int main()  
  32. {  
  33.     vector<int> B;  
  34.     for(int i = 0;i<5;i++) B.push_back(i);  
  35.     //bind2nd将一个仿函数和一个数值结合  
  36.     for_each(B.begin(), B.end(), bind2nd(A(),2));  
  37.     cout<<endl;  
  38.     //先用ptr_fun把一个普通函数适配成一个仿函数,再用bind2nd将之与常数结合  
  39.     for_each(B.begin(), B.end(), bind2nd(ptr_fun(fun),1));  
  40.     cout<<endl;  
  41.     //bind2nd将一个仿函数与普通函数结合  
  42.     for_each(B.begin(), B.end(), bind2nd(A(),Get(3)));  
  43. }  
注意在使用中注意一下几点:

一、在自己编写适用于bind2nd或bind1nd的仿函数时,该仿函数类必须继承于类binary_function(T(第一个参数类型),T(第二个参数类型),T(返回值类型))。

二、对于普通函数也需要用函数配接器结合时,需要先用ptr_fun()封装(对于ptr_fun的讲解在http://blog.csdn.net/yyc1023/article/details/38498325)。

至于其他的几个配接器的用法类似即可。



ptr_fun是将一个普通的函数适配成一个仿函数(functor), 添加上argument_type和result type等类型,它的定义如下:

[cpp]  view plain  copy
 print ?
  1. template<class _Arg1,  
  2.     class _Arg2,  
  3.     class _Result> inline  
  4.     pointer_to_binary_function<_Arg1, _Arg2, _Result,  
  5.         _Result(__clrcall *)(_Arg1, _Arg2)>  
  6.             ptr_fun(_Result (__clrcall *_Left)(_Arg1, _Arg2))  
  7.     {    // return pointer_to_binary_function functor adapter  
  8.     return (pointer_to_binary_function<_Arg1, _Arg2, _Result,  
  9.         _Result (__clrcall *)(_Arg1, _Arg2)>(_Left));  
  10.     }  
下面的例子就是说明了使用ptr_fun将普通函数(两个参数, 如果有多个参数, 要改用boost::bind)适配成bind1st或bind2nd能够使用的functor,否则对bind1st或bind2nd直接绑定普通函数,则编译出错。
[cpp]  view plain  copy
 print ?
  1. #include <algorithm>    
  2. #include <functional>    
  3. #include <iostream>    
  4.   
  5. using namespace std;    
  6.   
  7. int sum(int arg1, int arg2)    
  8. {    
  9.     std::cout<< "arg1 = " << arg1 << std::endl;    
  10.     std::cout<< "arg2 = " << arg2 << std::endl;    
  11.   
  12.     int sum = arg1 + arg2;    
  13.     std::cout << "sum = " << sum << std::endl;    
  14.   
  15.     return sum;    
  16. }  
  17.   
  18. int main(int argc, char *argv[], char *env[])  
  19. {    
  20.     bind1st(ptr_fun(sum), 1)(2);        // the same as sum(1,2)    
  21.     bind2nd(ptr_fun(sum), 1)(2);        // the same as sum(2,1)    
  22.   
  23.     return 0;  
  24. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值