C++从键盘读取任意长度的数组,现总结如下:
//读取指定长度的数组 int main() { int n = 0; cin >> n; vector<int> p(n); for (int i = 0; i < n; i++) { cin >> p[i]; } for (int i = 0;i < p.size();i++) { cout << p[i]; } cout << endl; system("pause"); } //从键盘读入任意长度的数组 int main() { vector<int>nums; int num = 0; do { cin >> num; nums.push_back(num); } while (getchar() != ' '); for (int i = 0;i < nums.size();i++) { cout << nums[i]; } cout << endl; system("pause"); return 0; } //键盘输入字符串类型的整数,然后将其输出数组中 int main() { string str, temp; getline(cin, str); int i = 0; vector<int> p; stringstream input(str); while (input >> i) { p.push_back(i); } for (int i = 0;i < p.size();i++) { cout << p[i]; } cout << endl; system("pause"); return 0; }