** preface **本文是对在C++中以回车结束数组(不定长)输入的记录。

其实这个问题,在遥远的曾经应该是解决过,可是今天却忘了,网上搜索的一些方案也不太靠谱。

这告诉我,一些当时觉得简单的坑,或者刚学习的知识点,如果时间允许,最好还是记录下。

看似慢,实则节约了更多的时间。

直接上代码了,逻辑相当简单。

** 定长的数组 **12345678910111213141516171819202122#includeusing 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> test; cout<

** 不定长的数组 **12345678910111213141516171819202122232425#include #include using namespace std;int main(){ vector a; int i; for(i=0;;i++) { int tem; cin >> tem; a.push_back(tem); if(getchar()=='\n') //遇回车结束 break; } for(int j=0;j> test; cout<

** 参考 **c++输入一组整型数据 不知道长度 回车键结束 并将其存入数组当中学习C++ -> 向量(vector)c++中vector的用法详解