CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Unhappy Question about input iterator on the same input stream.

    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

  2. #2
    Join Date
    Dec 2002
    Posts
    287
    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

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    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.

  4. #4
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468
    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

  5. #5
    Join Date
    Dec 2002
    Posts
    287
    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

  6. #6
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Talking

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured