1

I tried implementing an example of stream iterators from page 107 of "The C++ Standard Library". I get stuck on this line:

copy (istream_iterator<string>(cin), istream_iterator<string>(), back_inserter(coll)); 

The program keeps reading data from the console here, but does not pass on to the next line. How do I continue past this point?

Justin
  • 24,288
  • 12
  • 92
  • 142
Ilya Shutman
  • 41
  • 1
  • 3
  • Ctrl+Z worked. Thanks. – Ilya Shutman May 10 '18 at 04:27
  • If one of the answers answered your question, you can [accept it](https://meta.stackexchange.com/a/5235/218012) by clicking on the check mark. If they didn't answer your question, can you comment to elaborate on what you are missing? – Justin Jul 06 '18 at 18:23

2 Answers2

3

From cppreference:

The default-constructed std::istream_iterator is known as the end-of-stream iterator. When a valid std::istream_iterator reaches the end of the underlying stream, it becomes equal to the end-of-stream iterator. Dereferencing or incrementing it further invokes undefined behavior

bold added

In other words, std::istream_iterator<string>(std::cin) keeps going until the end-of-input for std::cin. This doesn't happen at the end of the line, but at the end-of-file. In a console, there are specific commands to trigger the EOF:

In UNIX systems it is Ctrl+D, in Windows Ctrl+Z.

Justin
  • 24,288
  • 12
  • 92
  • 142
0

take for instance, if you would have made an input stream of int then you would have given input like - 45 56 45345 555 ....., so in all those cases, the reading operation of input stream would have returned a true value - while (cin>>var) { } the while statement will not stop if it is getting a valid input, so to stop the reading of characters, we gave it following input, ... 54 56 3545 | , and as soon as it receives a special character the while loop stops as the conditions returns false.

That's the same case for all other type of input streams as well.

So I assume you understand here why your string type input stream never stops taking input because every possible input can be considered string.

The solution to this problem is using "ctrl + D in UNIX" and "ctrl + Z in windows", as it gives NULL in condition of while loop which means false, hence stoping the reading of string input.

samar taj Shaikh
  • 1,165
  • 11
  • 18
  • This answer is hard to follow. From a brief skim, I believe in the first paragraph, you are saying that this behavior is no different from what happens when you write `while (cin >> var) {}`, but it's not entirely clear. – Justin May 10 '18 at 04:33
  • "as it appends a NULL which returns false" is wrong in so many ways. It "appends" an EOF, which is different from NUL, which is different from NULL, which doesn't "return false" – Justin May 10 '18 at 04:34
  • thanks for your valuable comments, I will work harder on my vocabulary from next time. – samar taj Shaikh May 10 '18 at 04:57
  • You could always try to edit this answer to address what I mentioned. Vocabulary isn't necessarily the problem, though. That first paragraph could use some formatting, for example, and it's a bit too wordy - it's a run-on sentence. As for the last part which is wrong, you can absolutely correct that as well. – Justin May 10 '18 at 05:01