zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    单模光纤与多模光纤的区别
    普通的单模光纤和单模光纤跳线是否可以达到万兆速度?
    原创:在局域网中,如何知道某个IP被占用 show arp
    如何知道局域网内哪些ip被占用----工具法Free IP Scanner
    如何知道局域网内哪些ip被占用
    科普知识普及
    电脑通电自动开机
    2.4G还是5G?带你选择最正确的路由器
    光纤收发器指示灯介绍
    100BASE-TX、100Base-FX等含义
  • 原文地址:https://www.cnblogs.com/MrLiuZF/p/14078395.html
Copyright © 2011-2022 走看看