• C++之compose1、compose2


    compose1和compose2均是用于参数合成

    比如:

    f(x)=3*x,g(y)=y+2,那么compose1(f(x),g(y))=(y+2)*3

    g1(x)=3*x,g2(x)=2*x,h(x)=a+b,那么compose2(h(x),g1(x),g2(x))=3*x+2*x

    大体效果就是上面这样,但我没有实现,因为这两个配接器并未纳入STL标准,是SGI STL的私产品。STL的版本很多,常见的有HP STL、PJ STL、 SGI STL等。

    compose1源码

     1 template <class Operation1, class Operation2>
     2 class unary_compose : public unary_function<typename Operation2::argument_type,
     3                                             typename Operation1::result_type> {
     4 protected:
     5   Operation1 op1;
     6   Operation2 op2;
     7 public:
     8   unary_compose(const Operation1& x, const Operation2& y) : op1(x), op2(y) {}
     9   typename Operation1::result_type
    10   operator()(const typename Operation2::argument_type& x) const {
    11     return op1(op2(x));
    12   }
    13 };
    14  
    15 template <class Operation1, class Operation2>
    16 inline unary_compose<Operation1, Operation2> compose1(const Operation1& op1, 
    17                                                       const Operation2& op2) {
    18   return unary_compose<Operation1, Operation2>(op1, op2);
    19 }

    对外接口是函数compose1,实际实现是函数对象unary_compose。

    对外接口compose1接受两个参数,代表两个操作,操作op1的返回值类型作为合并后的一元函数对象 unary_compose的返回值型别,操作op2的参数类型作为合并后的一元函数对象unary_compose中重载函数()的参数型别。

     compose2源码

     1 template <class Operation1, class Operation2, class Operation3>
     2 class binary_compose
     3   : public unary_function<typename Operation2::argument_type,
     4                           typename Operation1::result_type> {
     5 protected:
     6   Operation1 op1;
     7   Operation2 op2;
     8   Operation3 op3;
     9 public:
    10   binary_compose(const Operation1& x, const Operation2& y, 
    11                  const Operation3& z) : op1(x), op2(y), op3(z) { }
    12   typename Operation1::result_type
    13   operator()(const typename Operation2::argument_type& x) const {
    14     return op1(op2(x), op3(x));
    15   }
    16 };
    17  
    18 template <class Operation1, class Operation2, class Operation3>
    19 inline binary_compose<Operation1, Operation2, Operation3> 
    20 compose2(const Operation1& op1, const Operation2& op2, const Operation3& op3) {
    21   return binary_compose<Operation1, Operation2, Operation3>(op1, op2, op3);
    22 }

    对外接口是函数compose2,实际实现是函数对象binary_compose。

    对外接口compose1接受三个参数,代表三个操作,操作op1的返回值类型作为合并后的一元函数对象 unary_compose的 返回值型别,操作
    op2的参数类型作为合并后的一元函数对象binary_compose中重载函数()的参数型别。
    其实最关键的就是看构造函数和重载函数()的形参,就可以很轻松的理解了

    本文来自博客园,作者:Mr-xxx,转载请注明原文链接:https://www.cnblogs.com/MrLiuZF/p/14078395.html

  • 相关阅读:
    Codeforces Round #256 (Div. 2/B)/Codeforces448B_Suffix Structures(字符串处理)
    【android】优秀的UI资源站点集合
    升级iOS8系统后,保险箱Pro、私人保险箱、私密相冊打开就闪退的官方解决方式
    js产生随机数
    java实现各种数据统计图(柱形图,饼图,折线图)
    Matlab画图-非常具体,非常全面
    Lucene教程具体解释
    NAND FLASH
    Jenkins(二)
    iOS 本地通知
  • 原文地址:https://www.cnblogs.com/MrLiuZF/p/14078395.html
Copyright © 2020-2023  润新知