The difference between C++:istreambuf_iterator and Istream_iterator

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.