1、迭代器适配器
2、插入迭代器
#include <iostream>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;void showVec(const vector<int>& v)
{for (vector<int>::const_iterator it = v.begin(); it != v.end(); ++it){cout << *it << ' ';}cout << endl;
}void showList(const list<int>& l)
{for (list<int>::const_iterator it = l.begin(); it != l.end(); ++it){cout << *it << ' ';}cout << endl;
}
int main() {int a[] = {1, 2, 3, 4, 5};vector<int> v(a, a + 5);//v.push_back(6);back_insert_iterator<vector<int>> bii(v);*bii = 6; // 重载=运算符,在重载里面调用push_backshowVec(v);vector<int> v2;back_insert_iterator<vector<int>> bii2(v2);copy(v.begin(), v.end(), bii2);showVec(v2);back_inserter(v) = 7;showVec(v);copy(v.begin(), v.end(), back_inserter(v2));showVec(v2);list<int> l(a, a + 5);front_insert_iterator<list<int>> fii(l);fii = 0;showList(l);list<int> l2;copy(l.begin(), l.end(), front_inserter(l2));showList(l2);return 0;
}// 输出
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 1 2 3 4 5 6 7
0 1 2 3 4 5
5 4 3 2 1 0
3、IO流迭代器
#include <iostream>
#include <vector>
#include <algorithm>
#include <list>
#include <iterator>using namespace std;int main() {int a[] = {1, 2, 3, 4, 5};// 输出流迭代器vector<int> v(a, a + 5);copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));cout << endl;// 输入流迭代器vector<int> v2;// copy from cin to vectorcopy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(v2));// copy from vector to coutcopy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));return 0;
}
// 输出
1 2 3 4 5
1 5 6 8 4
^D
1 5 6 8 4