In C + +, a stream can also be seen as a container, so there are corresponding iterator to traverse the contents of the stream, including the two stream iterators described in this article: Istreambuf_iterator and Istream_iterator, The use and distinction of these two iterators can be illustrated by two pieces of code:
Example 1:istreambuf_iterator
#include <fstream> #include <iostream> #include <iterator>using namespace Std;int main () {Ifstream in ( "Test.cpp");istreambuf_iterator<char> ISB (in),end;ostreambuf_iterator<char> OSB (cout), while (isb!=end ) *osb++ = *isb++;cout<<endl;return 0;}
The meaning of this code is to read the contents of the Test.cpp and print to the terminal, the output of the original part of the Test.cpp to retain the format, let us look at another example:
Example 2:
#include <fstream> #include <iostream> #include <iterator>using namespace Std;int main () {Ifstream in ( "Test.cpp");istream_iterator<char> ISB (in),end;ostream_iterator<char> OSB (cout), while (Isb!=end) *osb+ + = *isb++;cout<<endl;return 0;}
The output of this code discards all the blanks in Test.cpp! So print a bunch of characters on the terminal. The distinction between these two iterator is also very simple, just remember that with "BUF" is closer to the bottom, so the original point to all the characters are read in.
The difference between C++:istreambuf_iterator and Istream_iterator