流迭代器通常有下面的幾種做用:spa
//利用流迭代器填充vector { ifstream in("test.txt"); istream_iterator<string> ibegin(in); istream_iterator<string> iend; vector<string> vec(ibegin, iend); copy(vec.begin(), vec.end(), ostream_iterator<string>(cout, "/n")); }
//利用輸入流填充vector { vector<string> vec; copy(istream_iterator<string>(cin), istream_iterator<string>(), back_inserter(vec)); sort(vec.begin(), vec.end()); copy(vec.begin(), vec.end(), ostream_iterator<string>(cout,"/n")); }
//利用流迭代器保存vector內容到文件 { ifstream in("test.txt"); istream_iterator<string> ibegin(in); istream_iterator<string> iend; vector<string> vec(ibegin, iend); ofstream out("testcopy.txt"); copy(vec.begin(), vec.end(), ostream_iterator<string>(out, "/n")); }