1. 一行输入未指定数量
输入
1 2 3 4 5
#include <bits/stdc++.h>
using namespace std;
int main() {
// 输入:1 2 3 4 5
string item;
while (cin >> item) {
cout << item << " ";
if (getchar() == '\n') break;
}
return 0;
}
输出:
1 2 3 4 5
2. 一行输入非空格分割
输入
1_2_3_4_5
#include <bits/stdc++.h>
using namespace std;
int main() {
//输入:1_2_3_4_5
string data;
getline(cin, data);
stringstream ss(data);
string item;
while (getline(ss, item, '_')) {
cout << item << ' ';
}
return 0;
}
输出:
1 2 3 4 5
3. 多行输入
输入:
2 6
1 2 3 4 5 6
7 8 9 10 11 12
#include <bits/stdc++.h>
using namespace std;
int main() {
// 输入:
//2 6
//1 2 3 4 5 6
//7 8 9 10 11 12
int rows, cols;
cin >> rows >> cols;
vector<vector<int>> data(rows);
for (int i=0; i<rows; ++i) {
for (int j=0; j<cols; ++j) {
int temp;
cin >> temp;
data[i].push_back(temp);
}
}
for (int i=0; i<rows; ++i) {
for (int j=0; j<cols; ++j) {
cout << data[i][j] << " ";
}
cout << endl;
}
return 0;
}
输出:
1 2 3 4 5 6
7 8 9 10 11 12
|