problem about istream_iterator

T

tong

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;
 
S

Satish

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
 
S

Satish

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,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top