运行代码:
//C++数组、向量和列表的练习
#include"std_lib_facilities.h"
int main()
try
{
int ii[10] = { 0,1,2,3,4,5,6,7,8,9 };
for (int i = 0; i < 10; i++)//把数组中的每个元素值加2
ii[i] += 2;
vector<int>vv(10);
for (int i = 0; i < 10; i++)
vv.push_back(i);
for (int i = 0; i < 10; i++)//把向量中的每个元素值加3
vv[i] += 3;
list<int>ll;
for (int i = 0; i < 10; i++)
ll.push_back(i);
for(int i=0;i<10;i++)//把列表中的每个元素值加5
{
list<int>::iterator p=ll.begin();
*p += 5;
ll.push_back(*p);
ll.pop_front();
}
int ii_copy[10] = {};//定义一新数组,用上面数组的值对其初始化
for (int i = 0; i < 10; i++)
ii_copy[i] = ii[i];
vector<int>vv_copy(10);//定义一新向量,用上面向量的值对其初始化
for (int i = 0; i < 10; i++)
vv_copy[i] = vv[i];
list<int>ll_copy;//定义一新列表,用上面列表的值对其初始化
list<int>::iterator p = ll.begin();
for (int i = 0; i < 10; i++,p++)
ll_copy.push_back(*p);
p = ll_copy.begin();//打印新列表的值
for (int i = 0; i < 10; i++, p++)
cout << *p<<" ";
return 0;
}
catch (exception& e) {
cerr << "error:" << e.what() << '\n';
keep_window_open();
return 1;
}
catch (...) {
cerr << "Oops:unknown exception!\n";
keep_window_open();
return 2;
}
运行结果: