C++ primer(第五版) 练习 3.41
题目:编写一段程序,用整型数组初始化一个vector对象。
答:
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
using std::begin;
using std::end;
int main()
{
int a[] = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 };
vector<int> v1(begin(a), end(a));
cout << "数组a内的元素为:" << " ";
for (auto c : a)
cout << c << " ";
cout << endl;
cout << "v1容器内的元素为:" ;
for (auto c : v1)
cout << c << " ";
cout << endl;
return 0;
}
执行结果: