9月12日
stream Iterators(流迭代器)
最近在学习 c++传说那个强大的STL。感觉真的是很强大的.. 让我惊喜地就是它的stream iterator
stream iterator有两种istream iterator ,ostream iterator.
//Creates an ostream iterator for ostream with the string delim as the delimiter between the values (note that delim has type const char*)
ostream_iterator<T> (ostream,delim)
//Creates an istream iterator for istream (and might read the first value)
istream_iterator<T> (istream)
copy (coll.gin(), coll.end(), ostream_iterator<int>(cout," /n"))
//把coll整个容器拷贝到输出流 打印出来
copy(istream_iterator<string>(cin),istream_iterator<string>(),ostream_iterator<string>(cout," /n"))
//把您输入的东西全部打印出来
记得这个市stream iterator 所以所有市stream的东西都可以和stream 联系起来
如 file stream文件流。
ofstream outf("outputfile")
copy(istream_iterator<string>(cin),istream_iterator<string>(),ostream_iterator<string>(outf," /n"))
//从输入流读入您输入的东西然后保存到outputfile
这个是写出文件流 ,如果要读入文件流 也同理....用 ifstream。。。
---------------------------------------------------------
用STL使得读写文件这个复杂的功能 很简单化了......
hoho。。。。。。
如果这个功能要用普通的代码实现的话 代码量起码是 20行以上....