STL之組合型函數適配器f(g(x)),f(g(x1,x2)),f(g(x),h(x)),f(g(x1),h(x2))

來源:互聯網
上載者:User

組合型函數分成以下幾種,自己寫函數適配器

f(g(elem))                  ------> compose_f_gxf(g(elem1,elem2))           ------> compose_f_gxyf(g(elem),h(elem))          ------> compose_f_gx_hxf(g(elem),h(elem2))         ------> compose_f_gx_hy...

下面是具體的例子,慢慢琢磨:

#include<iostream>#include<vector>#include<algorithm>#include<functional>#include<iterator>using namespace std;template<typename Op1,typename Op2>class compose_f_gx_t:public unary_function<typename Op2::argument_type,typename Op1::result_type>{private:Op1 op1;Op2 op2;public:compose_f_gx_t(const Op1& o1,const Op2& o2):op1(o1),op2(o2){}typename Op1::result_type operator()(const typename Op2::argument_type& x)const {return op1(op2(x));}};template<typename Op1,typename Op2>inline compose_f_gx_t<Op1,Op2>  compose_f_gx(const Op1& o1,const Op2& o2){return compose_f_gx_t<Op1,Op2>(o1,o2);}int main(){vector<int>v;for(int i=1;i<=9;++i){v.push_back(i);}for(vector<int>::iterator itera=v.begin();itera!=v.end();++itera){cout<<*itera<<" ";}cout<<endl;transform(v.begin(),v.end(),ostream_iterator<int>(cout," "),compose_f_gx(bind2nd(multiplies<int>(),5),bind2nd(plus<int>(),10)));cout<<endl;system("pause");return 0;}

 

#include<iostream>#include<vector>#include<algorithm>#include<functional>using namespace std;template<typename Op1,typename Op2,typename Op3>class compose_f_gx_hx_t:public unary_function<typename Op2::argument_type,typename Op1::result_type>{private:Op1 op1;Op2 op2;Op3 op3;public:compose_f_gx_hx_t(const Op1& o1,const Op2& o2,const Op3& o3):op1(o1),op2(o2),op3(o3){}typename Op1::result_type operator()(const typename Op2::argument_type& x)const{return op1(op2(x),op3(x));}};template<typename Op1,typename Op2,typename Op3>inline compose_f_gx_hx_t<Op1,Op2,Op3>compose_f_gx_hx(const Op1& o1,const Op2& o2,const Op3& o3){return  compose_f_gx_hx_t<Op1,Op2,Op3>(o1,o2,o3);}int main(){    vector<int>v;for(int i=1;i<=9;++i){v.push_back (i);}for(vector<int>::iterator itera=v.begin();itera!=v.end();++itera){cout<<*itera<<" ";}cout<<endl;vector<int>::iterator it;it=remove_if(v.begin(),v.end(),compose_f_gx_hx(logical_and<bool>(),bind2nd(greater<int>(),4),bind2nd(less<int>(),7)));v.erase(it,v.end());for(vector<int>::iterator iterb=v.begin();iterb!=v.end();++iterb){cout<<*iterb<<" ";}cout<<endl;system("pause");return 0;}

 

#include<iostream>#include<cctype>#include<algorithm>#include<string>#include<vector>#include<functional>using namespace std;template<typename Op1,typename Op2,typename Op3>class compose_f_gx_hy_t:public binary_function<typename Op2::argument_type,typename Op3::argument_type,typename Op1::result_type>{private:Op1 op1;Op2 op2;Op3 op3;public:compose_f_gx_hy_t(const Op1& o1,const Op2& o2,const Op3& o3):op1(o1),op2(o2),op3(o3){};typename Op1::result_type operator()(const typename Op2::argument_type& x,const typename Op3::argument_type& y)const{return op1(op2(x),op3(y));}};template<typename Op1,typename Op2,typename Op3>inline compose_f_gx_hy_t<Op1,Op2,Op3>  compose_f_gx_hy(const Op1& o1,const Op2& o2,const Op3& o3){return compose_f_gx_hy_t<Op1,Op2,Op3>(o1,o2,o3);}int main(){string s("Internationalization");string sub("Nation");string::iterator it;it=search(s.begin(),s.end(),sub.begin(),sub.end(),compose_f_gx_hy(equal_to<int>(),ptr_fun(::toupper),ptr_fun(::toupper)));if(it!=s.end()){cout<<"\""<<sub<<"\" is part of\""<<s<<"\""<<endl;}system("pause");return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.