Question

Please consider the code below:

vector<__int64> vNumbers;   
copy(istream_iterator<__int64>(cin), istream_iterator<__int64>(), back_inserter(vNumbers));

If I'll enter 1 23 45 and press Enter the vector will be filled with 45, 23, 1, numbers. Now a simple question. How can I change the code so that if I press Enter and enter new numbers the numbers were added to the vector too? In other words how to configure end of stream iterator?

Was it helpful?

Solution

The problem you have is that the stream reads as far as it can without blocking again. As soon as you press [Enter], stdin gets flushed by the operating system (in some cases, not in all cases). The stream reads until EOF or until there is nothing to be read anymore. This is what occurs on every flush (bunch of new data available at once).

That's why different people observe different behaviour. As soon as you pipe a file into the stream, it will have the whole file available at once and only stop at the end of the file. The newlines is just whitespace to the stream, alike spaces and tab.

You cannot solve this problem without controlling the input at a lower level.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top