Topic: istreambuf_iterator difficult to use
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/06/17 Raw View
VSD wrote:
>
> why say's the standard that a istreambuf_iterator does not
> guarantee equality ??
>
> This means i can't wirte code like that
>
> istream_iterator it ;
> it = find(itBegin,itEnd,Value);
>
> because it == ++it will be true.
The only value you can use as an end iterator for an algorithm on an
istreambuf is the end-of-stream iterator value. Equality tests using
that iterator value will work.
> it is a totaly diffrent behavior as the string::iterator
> why ??
Because istreambuf_iterator is an input iterator, not a random-access
iterator. Each time you increment any iterator on a give streambuf, it
advances the streambuf's current position. Thus, if i and j are two such
iterators, both pointing at position 1, then i++ advances the current
position to 2, and changes i to point to position 2. If you then execute
j++, it advances the current position to 3, and changes j to point to
position 3.
This is a disadvantage of using the iterator abstraction to encapsulate
stream input; however the advantages of that idea far outweigh the
disadvantages - it just requires some care when coding.