*Copyright(c) 2016.煙臺大學計算機與控制工程學院
*ALL rights reserved.
*文件名稱:main.cpp
*作者:孫亞茹
*完成日期:2016年6月9日
*問題描述:閱讀程序,解釋執行結果。
*//
#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;
}
程序分析:
由list<int> id(ia, ia+4);知id裏面的數是1 2 3 4,所以第一次輸出爲1 2 3 4.
copy(ia+1, ia+2, front_inserter(id));區間前閉後開,就是把2插入1234前面,結果爲:2 1 2 3 4.
copy(ia+3, ia+4, back_inserter(id));是將4插在21234後面,結果爲:2 1 2 3 4 4.
list<int>::iterator ite = find(id.begin(), id.end(), 3);找到位置3,copy(ia+0, ia+2, inserter(id, ite));將1 2插到3的前面,結果爲:2 1 2 1 2 3 4 4.
copy(id.begin(), id.end(), outite);倒序輸出,結果爲:4 4 3 2 1 2 1 2.