C++ primer(第五版) 练习 3.32 个人code
题目:将上一题(练习3.31)刚刚创建的数组拷贝给另外一个数组。利用vector 重写程序,实现类似的功能。
答:
#include <iostream>
#include <vector>
#include <cstddef>
using std::cout;
using std::endl;
using std::vector;
int main()
{
int v1[10] = {};
int v2[10] = {};
vector<int> vt1;
int index = 0;
for (auto &a : v1)
a = index++;
cout << "V1的元素内容为:" << endl;
for (auto b : v1)
cout << b << " ";
cout << endl;
//拷贝v1给v2
for (size_t i = 0; i < 10; ++i)
{
v2[i] = v1[i];
}
cout << "v2的元素内容为:" << endl;
for (auto &c : v2)
cout << c << " ";
cout << endl;
//把v1的值拷贝给 vector vt1
for (auto &d : v1)
vt1.push_back(d);
cout << "vt1容器内元素的值为:" << endl;
for (auto e : vt1)
cout << e << " ";
cout << endl;
return 0;
}
执行结果: