problem about istream_iterator

Discussion in 'C++' started by tong, Jan 10, 2007.

  1. tong

    tong Guest

    i have tried a program that it should show all words i typed, but it can't
    e.g.
    1 2 3
    1 <--
    2 <--
    it always miss the last one
    then i continue to type
    4
    3 <-- it shows now !!

    However, if i change the loop into
    while( in_iter != eof){
    *out_iter++ = *in_iter;
    ++in_ter;
    }

    it will be fine, i don't know what happens to it.



    *****************************************************************
    #include <iostream>
    #include <iterator>
    #include <string>

    using namespace std;

    int main()
    {
    ostream_iterator<string> out_iter(cout, "<\n");
    istream_iterator<string> in_iter(cin), eof;

    while( in_iter != eof ){
    *out_iter++ = *in_iter++;
    }
    return 0;
     
    tong, Jan 10, 2007
    #1
    1. Advertising

  2. tong

    Satish Guest

    This is similar to
    cout << *in_iter++;

    Basically 3 things happen here
    1. cout << operator is passed the value
    2, increment operation takes place
    3. message is flushed to output stream.

    Tge problem is in the 2nd step when it increments and if it moves to
    beginning of next stream input after all buffer reading is completed
    and it goes to waiting state.
    , then the 3rd step is not executed

    Thanks,
    Satish

    tong wrote:
    > i have tried a program that it should show all words i typed, but it can't
    > e.g.
    > 1 2 3
    > 1 <--
    > 2 <--
    > it always miss the last one
    > then i continue to type
    > 4
    > 3 <-- it shows now !!
    >
    > However, if i change the loop into
    > while( in_iter != eof){
    > *out_iter++ = *in_iter;
    > ++in_ter;
    > }
    >
    > it will be fine, i don't know what happens to it.
    >
    >
    >
    > *****************************************************************
    > #include <iostream>
    > #include <iterator>
    > #include <string>
    >
    > using namespace std;
    >
    > int main()
    > {
    > ostream_iterator<string> out_iter(cout, "<\n");
    > istream_iterator<string> in_iter(cin), eof;
    >
    > while( in_iter != eof ){
    > *out_iter++ = *in_iter++;
    > }
    > return 0;
     
    Satish, Jan 10, 2007
    #2
    1. Advertising

  3. tong

    Satish Guest

    Just one correction though, I am not sure of the 1st step but what I am
    sure is before the message is flushed, the input iterator is
    incremented. For example

    cout << i++ << j++ << endl;//i++, j++ are all executed before message
    is flushed out.

    Thanks,
    Satish
    Satish wrote:
    > This is similar to
    > cout << *in_iter++;
    >
    > Basically 3 things happen here
    > 1. cout << operator is passed the value
    > 2, increment operation takes place
    > 3. message is flushed to output stream.
    >
    > Tge problem is in the 2nd step when it increments and if it moves to
    > beginning of next stream input after all buffer reading is completed
    > and it goes to waiting state.
    > , then the 3rd step is not executed
    >
    > Thanks,
    > Satish
    >
    > tong wrote:
    > > i have tried a program that it should show all words i typed, but it can't
    > > e.g.
    > > 1 2 3
    > > 1 <--
    > > 2 <--
    > > it always miss the last one
    > > then i continue to type
    > > 4
    > > 3 <-- it shows now !!
    > >
    > > However, if i change the loop into
    > > while( in_iter != eof){
    > > *out_iter++ = *in_iter;
    > > ++in_ter;
    > > }
    > >
    > > it will be fine, i don't know what happens to it.
    > >
    > >
    > >
    > > *****************************************************************
    > > #include <iostream>
    > > #include <iterator>
    > > #include <string>
    > >
    > > using namespace std;
    > >
    > > int main()
    > > {
    > > ostream_iterator<string> out_iter(cout, "<\n");
    > > istream_iterator<string> in_iter(cin), eof;
    > >
    > > while( in_iter != eof ){
    > > *out_iter++ = *in_iter++;
    > > }
    > > return 0;
     
    Satish, Jan 11, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill Rudolph

    istream_iterator question

    Bill Rudolph, Aug 14, 2003, in forum: C++
    Replies:
    4
    Views:
    560
    Bill Rudolph
    Aug 15, 2003
  2. Chris Mantoulidis
    Replies:
    2
    Views:
    429
  3. Alex Vinokur

    istream_iterator & copying files

    Alex Vinokur, Apr 19, 2004, in forum: C++
    Replies:
    10
    Views:
    1,256
    Alex Vinokur
    Apr 20, 2004
  4. dragoncoder

    Problem with std::istream_iterator

    dragoncoder, Sep 18, 2006, in forum: C++
    Replies:
    8
    Views:
    514
    Jerry Coffin
    Sep 18, 2006
  5. Pradeep
    Replies:
    5
    Views:
    1,107
    =?ISO-8859-1?Q?Jens_M=FCller?=
    Oct 19, 2006
Loading...

Share This Page