RT,众所周知普通的 max 函数是这样写的:
#define _cst const
template<typename T>inline T qmax(_cst T &a,_cst T &b){return a>b?a:b;}
如果我们想要实现 max(a,b,c,...),那么可以写出如下内容:
template<typename T,typename ...Arg>inline T qmax(_cst T &a,_cst T &b,_cst Arg &...arg){return qmax(a,qmax(b,arg...));}
可以看出,这个函数是递归调用的模板应用。
那么问题来了:对于一个数量比较多的情况,用 max(max(max(a,b),max(c,d)),max(max(e,f),max(g,h))) 这样的方法,递归层数是严格小于 max(max(max(...max(a,b),c),...)) 这样做的。而递归似乎速度比较……慢,常数大一些(?
那么请问有没有一种实现方法来完成上述那个 max(max(max(a,b),max(c,d)),max(max(e,f),max(g,h))) 呢?跪求大佬解答,谢谢。