除了为每个容器定义的迭代器之外,标准库在头文件iterator中还定义了额外几种迭代器,包括:
插入迭代器(insert iterator):这些迭代器被绑定到一个容器上,可用来向容器中插入元素;
插入器有三种类型
back_inserter
front_inserter
insert
流迭代器(stream iterator): 这些迭代器被绑定到输入或输出流上,可用来便利所有关联的IO流;
istream_iterator
ostream_itrator
#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之外的标准库都有反向迭代器;
移动迭代器(move iterator):这些专用的迭代器不是拷贝其中的元素,而是移动它们。
来源:oschina
链接:https://my.oschina.net/u/2445348/blog/651630