10
  1. What is the difference betwen istreambuf_iterator and istream_iterator.

  2. For the following code:

     istream_iterator<int> cin_in(cin);
     istream_iterator<int> end; 
    

    where does the iterator end point to?
    Will the iterator bind with the stream?
    If I write the code

    istream_iterator<int>()
    

    is it the same as end?

  3. And where is that all documented?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Jessica Jin
  • 195
  • 1
  • 7
  • 1
    possible duplicate of [Where do I find the current C or C++ standard documents?](http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents) – Deduplicator Nov 09 '14 at 02:24
  • @Deduplicator hi, thanks for replying.. I have read the link you include in the comment and I find there are so many answers there and I do not know which one is helpful... I am a new in C++ and it is a little bit difficult for me to read too complicated stuff.. – Jessica Jin Nov 09 '14 at 02:26
  • Also see: http://en.cppreference.com/w/cpp/iterator/istream_iterator `end` is known as a sentinel, it does not point anywhere specific. – Deduplicator Nov 09 '14 at 02:27
  • @Deduplicator and is it binded with the stream? since I find the code : `std::vector v( (std::istreambuf_iterator(in)), std::istreambuf_iterator() );` I do not quite understand the second parameter, `std::istreambuf_iterator()` , where is it pointing to? – Jessica Jin Nov 09 '14 at 02:29
  • see also https://stackoverflow.com/questions/10564013/c-streams-confusion-istreambuf-iterator-vs-istream-iterator – Chen Li Nov 08 '18 at 04:39
  • and https://stackoverflow.com/questions/8116541/what-exactly-is-streambuf-how-do-i-use-it – Chen Li Nov 08 '18 at 04:42

1 Answers1

15

What is the difference betwen istreambuf_iterator and istream_iterator.

std::istream_iterator is an iterator for formatted extraction. For instance, if you have a line of integers from a file and wish to copy them to some container, you would use std::istream_iterator<int> which internally will copy the value extracted from an int (using operator>>()) to the container:

std::copy(std::istream_iterator<int>(file),
          std::istream_iterator<int>(), std::back_inserter(some_container));

std::istreambuf_iterator is an iterator for unformatted extraction. It works directly on the std::streambuf object provided through its constructor. As such, if you need simply the contents of the file without worrying about their format, use this iterator. For example, sometimes you want to read an entire file into a string or some container. A regular formatted extractor will discard leading whitespace and convert extracted tokens; the buffer iterator will not:

std::string str(std::istreambuf_iterator<char>{file}, {});

Where does the iterator end point to?

A default-constructed stream iterator is simply a special sentinel object that represents the end of the stream. Since IOStreams are one-pass, there's no way for it to actually point to the end until we have read up to that point. Internally, when an extraction has failed or the read hit the end-of-file, the iterator that was constructed with a stream (or stream buffer) will change into an end stream iterator. This is what helps standard algorithms work with stream iterators, since they act like regular iterators on the outside.

And where is that all documented?

Many places. Formally in the Standard. You can also find documentation on cppreference.

David G
  • 94,763
  • 41
  • 167
  • 253