流迭代器一般有下面的几种作用:
//利用流迭代器填充vector
{
<span style="white-space:pre"> </span>ifstream in("test.txt");
<span style="white-space:pre"> </span>istream_iterator<string> ibegin(in);
<span style="white-space:pre"> </span>istream_iterator<string> iend;
<span style="white-space:pre"> </span>vector<string> vec(ibegin, iend);
<span style="white-space:pre"> </span>copy(vec.begin(), vec.end(), ostream_iterator<string>(cout, "/n"));
}
//利用输入流填充vector
{
<span style="white-space:pre"> </span>vector<string> vec;
<span style="white-space:pre"> </span>copy(istream_iterator<string>(cin), istream_iterator<string>(), back_inserter(vec));
<span style="white-space:pre"> </span>sort(vec.begin(), vec.end());
<span style="white-space:pre"> </span>copy(vec.begin(), vec.end(), ostream_iterator<string>(cout,"/n"));
}
//利用流迭代器保存vector内容到文件
{
<span style="white-space:pre"> </span>ifstream in("test.txt");
<span style="white-space:pre"> </span>istream_iterator<string> ibegin(in);
<span style="white-space:pre"> </span>istream_iterator<string> iend;
<span style="white-space:pre"> </span>vector<string> vec(ibegin, iend);
<span style="white-space:pre"> </span>ofstream out("testcopy.txt");
<span style="white-space:pre"> </span>copy(vec.begin(), vec.end(), ostream_iterator<string>(out, "/n"));
}