Read a sequence of ints from cin and store them in a vector

前端 未结 4 1560
粉色の甜心
粉色の甜心 2021-01-26 11:41

This is what I have done to read integers with std::cin and store them in a vector:

int number; 
vectorivec;

while (cin>>number)
{         


        
4条回答
  •  旧巷少年郎
    2021-01-26 12:15

    It depends on the terminal in use and the precise mechanism varies quite a lot but, conventionally, typing Ctrl+D (Linux) or Ctrl+Z (Windows) will result in an end-of-file "signal" being transmitted along the pipe, causing the EOF bit to be set on cin, and thus the next cin >> number attempt to fail.

    That will break the loop.

    Conveniently, the same will happen if you ran your executable with redirected input from a file. Which is kind of the point.

提交回复
热议问题