1、istream_iterator 读取输入流
ostream_iterator 读取输出流
2、几个程序
(1)
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
istream_iterator<int> in_iter(cin); //将迭代器与输入流绑定
istream_iterator<int> eof; //超出末端迭代器
vector<int> vec(in_iter,eof); //该行代码等价于下面三行代码
/**vector<int> vec;
while(in_iter!=eof)
vec.push_back(*in_iter++);
**/
vector<int>::iterator iter = vec.begin();
while(iter!=vec.end())
{
cout<<*iter<<endl;
++iter;
}
//cout << "Hello world!" << endl;
return 0;
}
(2)
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
istream_iterator<int> input(cin),eof;
vector<int> vec(input,eof); //输入系列数字并保存在vec中,直到输入非数字或遇到流结束符
sort(vec.begin(),vec.end());
ostream_iterator<int> out_iter(cout," ");
unique_copy(vec.begin(),vec.end(),out_iter);
return 0;
}