C++ 输入一行未知个数的整数
代码:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> inputs;
int n = 0, k=0;
int i;
cin >> i;
inputs.push_back(i);
while (cin.get() != '
') {
cin >> i;
inputs.push_back(i);
}
for (int i = 0; i < inputs.size(); i++)
{
cout<<inputs[i]<<" ";
}
return 0;
}
输入:
1 2 3
输出:
1 2 3
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(void) {
int num;
vector<int> v;
while (cin >> num) {
v.push_back(num);
char ch = getchar();//读取下一个字符,为换行符,则break
if (ch == '
')
break;
}
system("pause");
return 0;
}