Originally posted by Philip Nicoletti
I don't know if this will help or not ...
1) There are 5 CATEGORIES of iterators, one of
which is an input iterator. An input iterator
is anything that "acts like one", that is - has
the properties that are stated in the standard.
2) The standard supplies istream_iterator - an
iterator ADAPTER. It has the properties of an
input iterator along with others (the statements
that start "istream_iterator<...>"). I don't
think that inheritance is the correct concept
between istream_iterator and general input iterators,
but I could be wrong about that.
3) Look at the following code. Note: I changed the
contents of the input file as shown in the comments.
As the Josuttis book said, the iterators are equal
if they point to the same stream (and not end-of-stream),
or are both end of stream.
Code:
#include <iterator>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
// c:\\file1.txt: 0 1 2 3 4 5 6 7 8 9
ifstream ifile ("c:\\file1.txt");
istream_iterator<int> r (ifile);
istream_iterator<int> s (ifile);
istream_iterator<int> t;
cout << *r << endl;
cout << *r << endl;
cout << *r << endl;
cout << *s << endl;
(r==s) ? cout << "equal" : cout << "not equal";
cout << endl;
++r;
++s;
++r;
++s;
++s;
++s;
++s;
++s;
cout << *r << endl;
cout << *s << endl;
cout << *s << endl;
(r==s) ? cout << "r equals s" : cout << "r not equal to s" ;
cout << endl;
(t==s) ? cout << "t equals s" : cout << "t not equal to s" ;
cout << endl;
cout << endl << "now reading past-end-of-stream" << endl << endl;
++s;
(r==s) ? cout << "r equals s" : cout << "r not equal to s" ;
cout << endl;
(t==s) ? cout << "t equals s" : cout << "t not equal to s" ;
cout << endl;
return 0;
}
4) I think one reason that you are not getting definitive
answers is that normally, a programmer doesn't do
the "++=s" etc himself. The reason for istream_iterator's
existence is to allow streams to be used in algorithms.
Personnaly, that is the only way that I do use them. That
is not to say there are not circumstances when you would
want to - but I just don't think they are that common.