Click to See Complete Forum and Search --> : Question about input iterator on the same input stream.


George2
February 4th, 2003, 10:13 PM
Hi, everyone!

Here is a simple STL program. It uses two input iterators
to use on the same input file. I do not understand why the
output is looking like this.

Here are the source codes,

--------
#include <iterator>
#include <fstream>
#include <iostream>

using namespace std;

void main()
{
// c:\\file1.txt: 0 1 2 3
ifstream ifile ("c:\\file1.txt");

istream_iterator<int> r (ifile);
istream_iterator<int> s (ifile);

(r==s) ? cout << "equal" : cout << "not equal";
cout << endl;

++r;
++s;

cout << *r << endl;
cout << *s << endl;

(r==s) ? cout << "equal" : cout << "not equal";
cout << endl;
}
--------

Here are the outputs,

--------
equal
2
3
equal
--------


Thanks in advance,
George

DanM
February 5th, 2003, 02:02 AM
George2, I suggest you start using a debugger and and step through the code to see what is really happening. This is the only way you are going to learn something. Otherwise, I'm almost sure we will still be getting question from you in a year form now :)

The following explanation is based on the actual MS istream_iterator code but I think a regular C++ book would give a better theoretical view.

The first istream_iterator constructor initializes the internal reference to the istream object with the one passed as parameter. The it calls _Getval to set the _Myval to the next item in the stream (0 in our case).
Then another istream_iterator object is constructed and the next item in the ifile stream is extracted (1 in this case).

The two iterators are compared. The equality is based on the streams they are pointing to and since they both point to ifile, they are equal.
The ++ operator gets the next item in the stream (=2) and stores into the _Myval member.

Calling again the ++ operator will return the next item (=3).

The equality of the iterators is checked again (already explained)

Hope this helps you a little bit.
Reading some books is helpfull but you still have to get that compiler working and use the debugger to step through the code (I think I'm repeating myself :) )...

Dan

Philip Nicoletti
February 5th, 2003, 04:28 AM
Just a follow-up ... Two istream iterators are equal if :


both are end-of-stream iterators and this can no longer be read, or

both can read and use the same stream



from "The C++ Standard Library" by Josuttis.

George2
February 5th, 2003, 06:19 AM
Thanks, DanM and Philip buddies!

I still have a question. Do you mean that r and s
share the same pointer (position) of the input
stream? I learned from your reply that it is. But
before I think each of them has a different pointer
(position) of the input stream.


Cheers,
George

DanM
February 5th, 2003, 11:06 AM
The iterators maintain only the reference to the original stream and the value extracted from the stream using the >> operator.
The current position is maintained by the stream itself. So as long as the iterators point to the same stream, the position in the stream is going to be the same.
Use the deubgger...
You'll get all these answers by yourself and you'll learn a lot more :)

Dan

George2
February 6th, 2003, 12:37 AM
Thanks, DanM buddies!

George
Originally posted by DanM
The iterators maintain only the reference to the original stream and the value extracted from the stream using the >> operator.
The current position is maintained by the stream itself. So as long as the iterators point to the same stream, the position in the stream is going to be the same.
Use the deubgger...
You'll get all these answers by yourself and you'll learn a lot more :)

Dan