最近帮师兄做笔试题,对于连续输入未知数目整数的时候,以前我一直用While(cin>>temp),但是这样做的话需要输入完之后在加上ctrl+z,才能结束输入,但是在做笔试的时候测试数据不可能这样做,导致会出现问题,后来终于找到一个方法解决这个问题,现在写下来防止以后遇到这个问题,也希望能帮助大家。
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> Input;
int temp;
while (cin>>temp)
{
Input.push_back(temp);
if (getchar() == '\n')
{
break;
}
}
system("pause");
return 0;
}