** preface **
本文是对在C++中以回车结束数组(不定长)输入的记录。
其实这个问题,在遥远的曾经应该是解决过,可是今天却忘了,网上搜索的一些方案也不太靠谱。
这告诉我,一些当时觉得简单的坑,或者刚学习的知识点,如果时间允许,最好还是记录下。
看似慢,实则节约了更多的时间。
直接上代码了,逻辑相当简单。
** 定长的数组 **
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include<iostream> using namespace std; int main() {
int a[999]; int i;
for(i=0;;i++) { cin>>a[i]; if(getchar()=='\n') break; } for(int j=0;j<i+1;j++) cout<<a[j]<<" "; cout<<endl; int test; cin >> test; cout<<test<<endl; return 0; }
|
** 不定长的数组 **
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include <iostream> #include <vector> using namespace std; int main() {
vector<int> a; int i;
for(i=0;;i++) { int tem; cin >> tem; a.push_back(tem); if(getchar()=='\n') break; } for(int j=0;j<i+1;j++) cout<<a[j]<<" "; cout<<a.size()<<endl; cout<<endl; int test; cin >> test; cout<<test<<endl; return 0; }
|
** 参考 **
c++输入一组整型数据 不知道长度 回车键结束 并将其存入数组当中
学习C++ -> 向量(vector)
c++中vector的用法详解