今天偵錯工具的時候,遇到了這樣一個問題
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++標準程式庫提供的一些結構如下:
template<typename Arg,typename Result>struct unary_function{ typedef Arg argument_type; typedef Result result_type;};template<typename Arg1,typename Arg2,typename Result>struct binary_function{ typedef Arg1 first_argument_type; typedef Arg2 second_argument_type; typedef Result result_type;};
not2也是一樣的道理,not2是用於一個含有兩個參數傳回值為bool的函數
pos=find_if(v.begin(),v.end(),not1(ptr_fun(check)));這樣是正確的。
#include<iostream>#include<string>#include<vector>#include<functional>#include<algorithm>using namespace std;bool check(int a){ if(a%2==0){ return true; }else{ return false; }}int main(){ int a[]={1,2,3,4,5,6,7,8,9,10}; vector<int>v(a,a+10); vector<int>::iterator it=find_if(v.begin(),v.end(),not1(ptr_fun(check))); cout<<*it<<endl; system("pause"); return 0;}
mem_fun_ref和men_fun是針對成員函數而設計的函數適配器
class Person{ public: void print() const{ ... }}int main(){ vector<Person>v; ... for_each(v.begin(),v.end(),men_fun_ref(&Person::print));//調用成員數}
class Person{ public: void print() const{ ... }}int main(){ vector<Person*>v; ... for_each(v.begin(),v.end(),men_fun(&Person::print));//調用成員數}
知道差別了嗎
#include<iostream>#include<vector>#include<algorithm>#include<functional>#include<cmath>using namespace std;template<typename T1,typename T2>struct fopow:public binary_function<T1,T2,T1>{T1 operator()(T1 base,T2 exp) const{return pow(base,exp);}};int main(){vector<int>v;for(int i=1;i<=9;++i){v.push_back(i);}transform(v.begin(),v.end(),ostream_iterator<int>(cout," "),bind1st(fopow<float,int>(),3));cout<<endl;transform(v.begin(),v.end(),ostream_iterator<int>(cout," "),bind2nd(fopow<float,int>(),3));cout<<endl;system("pause");return 0;}