What is the difference between istreambuf_iterator and istream_iterator?
And in general what is the difference between streams and streambufs?
I really can’t find any clear explanation for this so decided to ask here.
What is the difference between istreambuf_iterator and istream_iterator ? And in general what is
Share
IOstreams use streambufs to as their source / target of input / output. Effectively, the streambuf-family does all the work regarding IO and the IOstream-family is only used for formatting and to-string / from-string transformation.
Now,
istream_iteratortakes a template argument that says what the unformatted string-sequence from the streambuf should be formatted as, likeistream_iterator<int>will interpret (whitespace-delimited) all incoming text asints.On the other hand,
istreambuf_iteratoronly cares about the raw characters and iterates directly over the associated streambuf of theistreamthat it gets passed.Generally, if you’re only interested in the raw characters, use an
istreambuf_iterator. If you’re interested in the formatted input, use anistream_iterator.All of what I said also applies to
ostream_iteratorandostreambuf_iterator.