迭代的运行效率始终强于递归,递归始终比迭代方便开发。
变长模板属于C++11中比较复杂的技术,在此简单介绍下。
#include <iostream>
using namespace std;
template<class... Args>
int Sum (Args... args) {
return sizeof...(args);
}
int main (int argc, char* argv []) {
cout << Sum (1,2,23,124,4,23,43,24,32,4,23) << endl;
return 0;
}
这是一个最简单的变长模板,首先是template定义,class后面加三个点就代表不固定长度。
接下来是Sum函数,在main函数的调用中实例化,被扩展为如下形式:
int Sum(int,int,int,int,int,int,int,int,int,int,int)
传了11个参数,就被实例化为11个参数形式。然后是sizeof...,这个宏是用来获取变长模板中元素的个数用的。我们调用时传了11个参数,它就返回11。程序运行的结果也相应为11。变长模板对于变长参数的优势之一为第一个参数不用定义参数个数(关于变长参数的访问详见
深度研究C语言变长函数),但相应的劣势为无法直接访问参数。文章后面将简要介绍如何调用参数。
这是最简单的用法,接下来看看稍微复杂点的。
将参数进行一一比较,如果不为3,显示“值 is not 3”,如果为3则显示“find 3”然后立即返回。首先我们用递归来实现:
#include <iostream>
using namespace std;
template<class T, class... Args>
bool check (T t, Args... args) {
if (t == 3) {
cout << "find 3" << endl;
} else {
cout << t << " is not 3" << endl;
}
return (t != 3) && check (args...);
}
template<class T>
bool check (T t) {
if (t == 3) {
cout << "find 3" << endl;
} else {
cout << t << " is not 3" << endl;
}
return (t != 3);
}
int main (int argc, char* argv []) {
check (1,2,5,7,9,3,5,6);
return 0;
}
程序执行结果如下:
这个例子的代码还算比较简洁的。但变长模板展开后是什么情况呢?就像下面这样:
#include <iostream>
using namespace std;
bool check (int i1) {
if (i1 == 3) {
cout << "find 3" << endl;
} else {
cout << i1 << " is not 3" << endl;
}
return (i1 != 3);
}
bool check (int i1, int i2) {
if (i1 == 3) {
cout << "find 3" << endl;
} else {
cout << i1 << " is not 3" << endl;
}
return (i1 != 3) && check (i2);
}
bool check (int i1, int i2, int i3) {
if (i1 == 3) {
cout << "find 3" << endl;
} else {
cout << i1 << " is not 3" << endl;
}
return (i1 != 3) && check (i2, i3);
}
bool check (int i1, int i2, int i3, int i4) {
if (i1 == 3) {
cout << "find 3" << endl;
} else {
cout << i1 << " is not 3" << endl;
}
return (i1 != 3) && check (i2, i3, i4);
}
bool check (int i1, int i2, int i3, int i4, int i5) {
if (i1 == 3) {
cout << "find 3" << endl;
} else {
cout << i1 << " is not 3" << endl;
}
return (i1 != 3) && check (i2, i3, i4, i5);
}
bool check (int i1, int i2, int i3, int i4, int i5, int i6) {
if (i1 == 3) {
cout << "find 3" << endl;
} else {
cout << i1 << " is not 3" << endl;
}
return (i1 != 3) && check (i2, i3, i4, i5, i6);
}
bool check (int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
if (i1 == 3) {
cout << "find 3" << endl;
} else {
cout << i1 << " is not 3" << endl;
}
return (i1 != 3) && check (i2, i3, i4, i5, i6, i7);
}
bool check (int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8) {
if (i1 == 3) {
cout << "find 3" << endl;
} else {
cout << i1 << " is not 3" << endl;
}
return (i1 != 3) && check (i2, i3, i4, i5, i6, i7, i8);
}
int main (int argc, char* argv []) {
check (1,2,5,7,9,3,5,6);
return 0;
}
乍一看,简直吓尿。完全是新手写的代码。事实就是这样,所有通过递归实现的变长模板展开都是这样。上面的例子调用8个参数的重载,然后它调用7个函数的重载,然后调用6个函数的重载……
效果是实现了,可是这代码被扩展了这么多次,太浪费了。有没更好的实现方式呢?有,通过迭代。以下示例通过迭代展开变长模板:
#include <iostream>
#include <vector>
#include <functional>
using namespace std;
template<class... Args>
void fun (Args... args) {
function<bool (int)> check = [] (int t) {
if (t == 3) {
cout << "find 3" << endl;
} else {
cout << t << " is not 3" << endl;
}
return t != 3;
};
vector<function<bool ()>> fun = { bind (check, args)... };
for (auto f : fun) {
if (!f ()) break;
}
}
int main (int argc, char* argv []) {
fun (1,2,5,7,9,3,5,6);
return 0;
}
代码执行结果与上面的递归的执行结果相同。逻辑貌似比上面更不易懂,我们依次来分析。首先是fun函数,由于不是一次一次的取,所以不用写两次重载,比迭代稍微方便些。然后是一个check函数lambda表达式,这个表达式只是为了让check函数只能在fun函数内访问,减少公开不必要的接口。重点不在这,重点在下面一行,vector这行。这行使用了三个C++11专有特性。
第一个是bind,用于函数的地址与参数绑定。为何需要绑定?因为变长模板扩的迭代扩展首先就是初始化逗号表达式。如果这那行像下面这样写:
bool val[] = { check(args)... };
变长模板将会将其扩展为如下形式:
bool val[] = { check(arg0), check(arg1), check(arg2), check(arg3), check(arg4), check(arg5), check(arg6), check(arg7) };
省略号将在扩展时一次性全部扩展,这样导致的结果就是初始化时被全部调用,不能实现调用到3之后立即返回的功能。
而上面的std::bind函数恰好解决了这个问题,它可以将函数或仿函数与参数绑定,返回一个内部仿函数,这样,调用这个内部仿函数,就能调用原始的函数并传入绑定的参数了。
上面的代码模板扩展之后长这样:
#include <iostream>
#include <vector>
#include <functional>
using namespace std;
void fun (int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8) {
function<bool (int)> check = [] (int t) {
if (t == 3) {
cout << "find 3" << endl;
} else {
cout << t << " is not 3" << endl;
}
return t != 3;
};
vector<function<bool ()>> fun = {
bind (check, i1),
bind (check, i2),
bind (check, i3),
bind (check, i4),
bind (check, i5),
bind (check, i6),
bind (check, i7),
bind (check, i8)
};
for (auto f : fun) {
if (!f ()) break;
}
}
int main (int argc, char* argv []) {
fun (1,2,5,7,9,3,5,6);
return 0;
}
这儿还涉及到一个问题,那就是C++11的初始化列表。能让vector或自定义类实现{xxx,xxx}这样的初始化。这样就不用一个一个push_back了。
然后,接下来是基于范围的for循环了。依次取每个绑定,由于绑定自带参数,所以这儿就不用传参了(注意vector的声明,函数返回值为bool但没有参数)。如果bool返回假那么退出循环,将不会调用后面的代码。
这只是一个简单的实现,可见迭代的代码效率优势在任何时候都是强于递归的。
最后说一个需要注意到的地方。变长模板扩展不是你想扩展就能扩展,比如扩展成这样 check(0)&&check(1)&&check(2)…… 貌似很好看,实际上你是想多了。一次性扩展还只能用逗号实现,否则上面代码就不会拐个弯了。如果C++17能实现这样的扩展就好玩了……