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源码
template <class Operation1, class Operation2> class unary_compose : public unary_function<typename Operation2::argument_type, typename Operation1::result_type> { protected: Operation1 op1; Operation2 op2; public: unary_compose(const Operation1& x, const Operation2& y) : op1(x), op2(y) {} typename Operation1::result_type operator()(const typename Operation2::argument_type& x) const { return op1(op2(x)); } }; template <class Operation1, class Operation2> inline unary_compose<Operation1, Operation2> compose1(const Operation1& op1, const Operation2& op2) { return unary_compose<Operation1, Operation2>(op1, op2); }
对外接口是函数compose1,实际实现是函数对象unary_compose。
对外接口compose1接受两个参数,代表两个操作,操作op1的返回值类型作为合并后的一元函数对象 unary_compose的返回值型别,操作op2的参数类型作为 合并后的一元函数对象unary_compose中重载函数()的参数型别。compose2源码
template <class Operation1, class Operation2, class Operation3> class binary_compose : public unary_function<typename Operation2::argument_type, typename Operation1::result_type> { protected: Operation1 op1; Operation2 op2; Operation3 op3; public: binary_compose(const Operation1& x, const Operation2& y, const Operation3& z) : op1(x), op2(y), op3(z) { } typename Operation1::result_type operator()(const typename Operation2::argument_type& x) const { return op1(op2(x), op3(x)); } }; template <class Operation1, class Operation2, class Operation3> inline binary_compose<Operation1, Operation2, Operation3> compose2(const Operation1& op1, const Operation2& op2, const Operation3& op3) { return binary_compose<Operation1, Operation2, Operation3>(op1, op2, op3); }
对外接口compose1接受三个参数,代表三个操作,操作op1的返回值类型作为合并后的一元函数对象
unary_compose的
返回值型别,操作
op2的参数类型作为合并后的一元函数对象binary_compose中重载函数()的参数型别。
其实最关键的就是看构造函数和重载函数()的形参,就可以很轻松的理解了