除了为每一个容器定义的迭代器以外,标准库在头文件iterator中还定义了额外几种迭代器,包括:ios
插入迭代器(insert iterator):这些迭代器被绑定到一个容器上,可用来向容器中插入元素;post
插入器有三种类型this
back_inserterspa
front_insertercode
insert
ci
流迭代器(stream iterator): 这些迭代器被绑定到输入或输出流上,可用来便利全部关联的IO流;element
istream_iteratorrem
ostream_itratorget
#include <list> #include <vector> #include <iostream> #include <iterator> #include <algorithm> using namespace std; void main() { vector<int> vec; istream_iterator<int> in_iter(cin); // read ints from cin istream_iterator<int> eof; // istream ``end'' iterator // use in_iter to read cin storing what we read in vec while (in_iter != eof) // while there's valid input to read // postfix increment reads the stream and // returns the old value of the iterator // we dereference that iterator to get // the previous value read from the stream vec.push_back(*in_iter++); // use an ostream_iterator to print the contents of vec ostream_iterator<int> out_iter(cout, " "); copy(vec.begin(), vec.end(), out_iter); cout << endl; // alternative way to print contents of vec for (auto e : vec) *out_iter++ = e; // the assignment writes this element to cout cout << endl; system("pause"); }
反向迭代器(reverse iterator):这些迭代器向后而不是向前移动,除了firward_list以外的标准库都有反向迭代器;input
移动迭代器(move iterator):这些专用的迭代器不是拷贝其中的元素,而是移动它们。