函数配接器

来源:互联网 发布:阿里妈妈解绑淘宝账号 编辑:程序博客网 时间:2024/06/06 07:53

组合函数配接器的可能表现形式功能名称SGI STL的名称f(g(elem))compose_f_gxcompose1f(g(elem,elem2))compose_f_gxy f(g(elem),h(elem))compose_f_gx_hxcompose2f(g(elem),h(elem2))compose_f_gx_hy 看不懂请了解点击打开链接


1.一元组合函数配接器

#include<iostream>#include<algorithm>#include<functional>#include<vector>#include<string>using namespace std;//组合“一个运作规则”template<class op1,class op2>class compose_f_gx_t :public unary_function<typename op2::argument_type, typename op1::result_type>{private:op1 myop1;op2 myop2;public:compose_f_gx_t(const op1&o1, const op2&o2) :myop1(o1), myop2(o2){};typename op1::result_type operator()( const typename op2::argument_type &x){return myop1(myop2(x));}};//把类模板包装成内置模板函数template <class op1,class 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);}class sequence{private:int value;public:sequence(int n) :value(n){};int operator ()(){return value++;}};void main(){vector<int>vt;generate_n(back_inserter(vt), 10, sequence(1));copy(vt.begin(), vt.end(), ostream_iterator<int>(cout, ","));cout << endl;//实现了对容器中的每个元素先加10在乘5,并且输出到屏幕上transform(vt.begin(), vt.end(), ostream_iterator<int>(cout, "--"),compose_f_gx(bind2nd(multiplies<int>(), 5), bind2nd(plus<int>(), 10)));system("pause");}

//组合“两个运作规则”template <class op1,class op2,class op3>class compose_f_gxy_t :public unary_function<typename op1::result_type, typename op2::argument_type>{private:op1 myop1;op2 myop2;op3 myop3;public:compose_f_gxy_t(const op1 &o1, const op2 &o2, const op3 &o3) :myop1(o1), myop2(o2), myop3(o3){};typename op1::result_type operator()(const typename op2::argument_type &x){return myop1(myop2(x), myop3(x));}};template<class op1,class op2,class op3>inline compose_f_gxy_t<op1, op2, op3> compose_f_gxy(const op1 &o1, const op2&o2, const op3 &o3){return compose_f_gxy_t<op1, op2, op3>(o1, o2, o3);}void main(){vector<float>vt;generate_n(back_inserter(vt), 20, sequence(1));//赋值1-20copy(vt.begin(), vt.end(), ostream_iterator<float>(cout, ":"));cout << endl;vector<float>::iterator pr;//删掉小于10大于21的元素pr = remove_if(vt.begin(), vt.end(), compose_f_gxy(logical_and<bool>(), bind2nd(greater<float>(), 10), bind2nd(less<float>(), 21)));vt.erase(pr, vt.end());copy(vt.begin(), vt.end(), ostream_iterator<float>(cout, ":"));system("pause");}

2.二元组合函数配接器



#include<iostream>#include<algorithm>#include<functional>#include<vector>#include<string>using namespace std;template<class op1,class op2,class op3>class compose_f_gx_hx_t :public binary_function< typename op1::result_type, typename op2::argument_type, typename op3::argument_type >{private:op1 myop1;op2 myop2;op3 myop3;public:compose_f_gx_hx_t(const op1 &o1, const op2 &o2, const op3&o3) :myop1(o1), myop2(o2), myop3(o3){};typename op1::result_type operator()(const typename op2::argument_type&x, const typename op3::argument_type&y){return myop1(myop2(x), myop3(y));}};template<class op1,class op2,class 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);}void main(){string s = "Iternationlization";string b = "nation";string::iterator pr;//在指定字符串中搜索子串的功能pr = search(s.begin(), s.end(), b.begin(), b.end(), compose_f_gx_hx(equal_to<int>(), ptr_fun(::toupper), ptr_fun(::toupper)));if (pr != s.end()){cout << " " << b << "is the part of " << s << endl;}system("pause");}

0 0
原创粉丝点击