Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > problem about istream_iterator

Reply
Thread Tools

problem about istream_iterator

 
 
tong
Guest
Posts: n/a
 
      01-10-2007

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;



 
Reply With Quote
 
 
 
 
Satish
Guest
Posts: n/a
 
      01-10-2007
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;


 
Reply With Quote
 
 
 
 
Satish
Guest
Posts: n/a
 
      01-11-2007
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;


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem | Istream_iterator in std::copy Pradeep C++ 5 10-19-2006 04:51 PM
Problem with std::istream_iterator dragoncoder C++ 8 09-18-2006 10:27 PM
istream_iterator & copying files Alex Vinokur C++ 10 04-20-2004 07:16 PM
istream_iterator and ostream_iterator problem Chris Mantoulidis C++ 2 12-15-2003 12:14 PM
istream_iterator question Bill Rudolph C++ 4 08-15-2003 07:28 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57