练习6.33:编写一个递归函数,输出vector对象的内容。
【出题思路】
函数的递归分为直接递归和间接递归。编写递归函数的关键是确定递归规律和递归终止条件。
【解答】
满足题意的程序如下所示:
#include <iostream>
#include <vector>
using namespace std;
//递归函数输出vector<int〉的内容
void print (vector<int> vlnt, unsigned index)
{
unsigned sz = vlnt.size ();
if(!vlnt.empty() && index < sz)
{
cout << vlnt[index] << endl;
print(vlnt, index + 1);
}
}
int main()
{
vector<int> v = {1,3,5,7,9,11,13,15};
print(v, 0);
return 0;
}
本页共29段,1010个字符,1225 Byte(字节)