mem_fun_ref,mem_fun,not1,not2,ptr_fun

转自:http://blog.csdn.net/zh634455283/article/details/7791503

今天调试程序的时候,遇到了这样一个问题

bool check(int elem);

vecot<int>v;  ...

pos=find_if(v.begin(),v.end(),not1(check))竟然出错,查找资料之后,发现,原来原因如下:

ptr_fun做的唯一的事是使一些typedef有效(  仿函数类的operator()所带的参数的类型和它的返回类型。对于binary_function,你要指定三个类型:你的operator的第一个和第二个参数的类型,和你的operator地返回类型; 而这两个基类 typedef 了argument_type、first_argument_type、second_argument_type和result_type 这几种类型 )。就是这样。not1需要这些typedef,这就是为什么可以把not1应用于ptr_fun,但不能直接对check应用not1。因为是低级的函数指针,check缺乏not1需要的typedef。
not1不是STL中唯一有那些要求的组件。四个标准函数适配器(not1、not2、bind1st和bind2nd)都需要存在某些typedef

c++标准程序库提供的一些结构如下:

[cpp]  view plain copy
  1. template<typename Arg,typename Result>  
  2. struct unary_function{  
  3.     typedef Arg argument_type;  
  4.     typedef Result result_type;  
  5. };  
  6.   
  7. template<typename Arg1,typename Arg2,typename Result>  
  8. struct binary_function{  
  9.      typedef Arg1 first_argument_type;  
  10.      typedef Arg2 second_argument_type;  
  11.      typedef Result result_type;  
  12. };  


 

not2也是一样的道理,not2是用于一个含有两个参数返回值为bool的函数

[cpp]  view plain copy
  1. pos=find_if(v.begin(),v.end(),not1(ptr_fun(check)));这样是正确的。  
[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<string>  
  3. #include<vector>  
  4. #include<functional>  
  5. #include<algorithm>  
  6. using namespace std;  
  7. bool check(int a){  
  8.  if(a%2==0){  
  9.   return true;  
  10.  }else{  
  11.   return false;  
  12.  }  
  13. }  
  14. int main(){  
  15.  int a[]={1,2,3,4,5,6,7,8,9,10};  
  16.  vector<int>v(a,a+10);  
  17.  vector<int>::iterator it=find_if(v.begin(),v.end(),not1(ptr_fun(check)));  
  18.  cout<<*it<<endl;  
  19.  system("pause");  
  20.  return 0;  
  21. }  


 

mem_fun_ref和men_fun是针对成员函数而设计的函数适配器

[cpp]  view plain copy
  1. class Person{  
  2.   public:  
  3.      void print() const{  
  4.             ...  
  5.      }  
  6. }  
  7. int main(){  
  8.   vector<Person>v;  
  9.  ...  
  10.   for_each(v.begin(),v.end(),men_fun_ref(&Person::print));//调用成员数  
  11.   
  12. }  

 

[cpp]  view plain copy
  1. class Person{  
  2.   public:  
  3.      void print() const{  
  4.             ...  
  5.      }  
  6. }  
  7. int main(){  
  8.   vector<Person*>v;  
  9.  ...  
  10.   for_each(v.begin(),v.end(),men_fun(&Person::print));//调用成员数  
  11.   
  12. }  

知道差别了吗

[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<vector>  
  3. #include<algorithm>  
  4. #include<functional>  
  5. #include<cmath>  
  6. using namespace std;  
  7. template<typename T1,typename T2>  
  8. struct fopow:public binary_function<T1,T2,T1>  
  9. {  
  10.     T1 operator()(T1 base,T2 exp) const{  
  11.         return pow(base,exp);  
  12.     }  
  13. };  
  14. int main(){  
  15.       
  16.     vector<int>v;  
  17.     for(int i=1;i<=9;++i){  
  18.         v.push_back(i);  
  19.     }  
  20.     transform(v.begin(),v.end(),ostream_iterator<int>(cout," "),bind1st(fopow<float,int>(),3));  
  21.     cout<<endl;  
  22.   
  23.     transform(v.begin(),v.end(),ostream_iterator<int>(cout," "),bind2nd(fopow<float,int>(),3));  
  24.     cout<<endl;  
  25.     system("pause");  
  26.     return 0;  
  27. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值