STL ostream_iterator writes to screen even though I overwrote it?

Posted by adam_0 on Stack Overflow See other posts from Stack Overflow or by adam_0
Published on 2010-06-15T22:39:29Z Indexed on 2010/06/15 22:42 UTC
Read the original article Hit count: 121

Filed under:
|
|

In my code, I have the following:

ostream_iterator<double> doubleWriter(cout, " ~ ");
// ...
*doubleWriter = 1.1;
doubleWriter++;
*doubleWriter = 2.2;
*doubleWriter = 3.3; // shouldn't 2.2 be overwritten?
doubleWriter++;
*doubleWriter = 44.2;

cout << endl << endl;

I expected it to output this:

1.1 ~ 3.3 ~ 44.2 ~ 

Instead, the output was this:

1.1 ~ 2.2 ~ 3.3 ~ 44.2 ~ 

Why does this happen? It would seem to me that I overwrite 2.2 and stick 3.3 in its spot, since I didn't increment. Is incrementation an optional step?

© Stack Overflow or respective owner

Related posts about c++

Related posts about stl