阅读并运行程序,解释程序执行得到的结果
(1)
#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
int main()
{
vector<int> ivec;
int i;
for(i = 0; i < 5; i++ )
ivec.push_back(i);
for(i = 0; i < 5; i++)
cout<<ivec[i]<<" ";
cout<<endl;
while( !ivec.empty())
{
cout << ivec.back() << " ";
ivec.pop_back();
}
cout << endl;
for(i = 0; i < 5; i++)
cout<<ivec[i]<<" ";
cout<<endl;
copy(ivec.begin(),ivec.end(),ostream_iterator<int>(cout, " "));
cout<<endl;
cout << "size=" << ivec.size() << endl;
return 0;
}
(2)
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> intList;
vector<int>::iterator listIt;
int i;
intList.push_back(1);
intList.push_back(5);
intList.push_back(10);
intList.push_back(15);
cout<<"Line 1: List Elements: ";
for(i=0; i<4; i++)
cout<<intList[i]<<" ";
cout<<endl;
for(i=0; i<4; i++)
intList[i] *=2;
cout<<"Line 2: List Elements: ";
for(listIt=intList.begin(); listIt != intList.end(); ++listIt)
cout<<*listIt<<" ";
cout<<endl;
listIt=intList.begin();
++listIt;
++listIt;
intList.insert(listIt,8);
cout<<"Line 3: List Elements: ";
for(listIt = intList.begin(); listIt != intList.end(); ++listIt)
cout<<*listIt<<" ";
cout<<endl;
return 0;
}
(3)
#include <iterator>
#include <list>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int ia[5] = {1,2,3,4};
list<int> id(ia, ia+4);
ostream_iterator<int> outite(cout, " ");
copy(id.begin(), id.end(), outite);
cout << endl;
copy(ia+1, ia+2, front_inserter(id));
copy(id.begin(), id.end(), outite);
cout << endl;
copy(ia+3, ia+4, back_inserter(id));
copy(id.begin(), id.end(), outite);
cout << endl;
list<int>::iterator ite = find(id.begin(), id.end(), 3);
copy(ia+0, ia+2, inserter(id, ite));
copy(id.begin(), id.end(), outite);
cout << endl;
copy(id.rbegin(), id.rend(), outite);
cout << endl;
return 0;
}