Read a sequence of ints from cin and store them in

2020-05-06 11:00发布

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

int number; 
vector<int>ivec;

while (cin>>number)
{       
    ivec.push_back(number);
}

for (auto v: ivec){

    cout << v;
}

Then, I am stuck with the problem that how to stop entering integers and move to the next process of printing the vector out. Any pointer will be appreciated.

标签: c++
4条回答
Luminary・发光体
2楼-- · 2020-05-06 11:25

The code you posted reads numbers from cin as long as it succeeds. There are two ways to have it stop succeeding: If you enter something that is not a number, reading data still succeeds, but converting it to number fails. This puts cin into the bad state. It can be recovered from using the clear methode. The other way is making the reading of characters from cin fail (for example the end of a file that gets used as input). This puts cin into the failed state. Usually, recovering from a failed state is impossible.

To produce the you can no longer read state at end of file when entering data at the keyboard, operating system specific methods have to be used (likely Control-D or Control-Z). This is final for the invocation of your program.

If you need a way for the user to signal: "Please go on, but let me enter other stuff later", the most clean way is likely reading cin line-by-line and parse the input using strtol or a stringstream, and comparing for a magic stop-token (e.g. empty line, "end") to exit the loop.

查看更多
够拽才男人
3楼-- · 2020-05-06 11:26

You can do it the following way

#include <vector>
#include <iterator>

//...

vector<int> ivec( std::istream_iterator<int>( std::cin ), 
                  std::istream_iterator<int>() );


for ( auto v: ivec ) std::cout << v << ' ';
std::cout << std::endl;

In Windows you have to press key combination Ctrl + z and in UNIX system Ctrl + d

Or you can introduce a sentinel. In this case the loop can look like

int number; 

const int sentinel = SOME_VALUE;

for ( std::cin >> number && number != sentinel ) vec.push_back( number );
查看更多
萌系小妹纸
4楼-- · 2020-05-06 11:35

Please find a simple solution to your problem, let me know if you see any issue with this solution.

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> va;
    int x;
    while ( cin >> x ) {
          va.push_back(x);
          if ( cin.get() == '\n' ) break;
    }
    //Vector output
    for ( int i = 0; i < va.size(); i++ ) {
        cout << va[i] <<" ";
    }
    return 0;
}
查看更多
做自己的国王
5楼-- · 2020-05-06 11:44

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.

查看更多
登录 后发表回答