C++ 输入一行未知个数的整数
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() != '\n') {
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 == '\n')
break;
}
system("pause");
return 0;
}
因上求缘,果上努力~~~~ 作者:图神经网络,转载请注明原文链接:https://www.cnblogs.com/BlairGrowing/p/13492012.html