-
当不知道给定矩阵的行数和列数时,读取数据可以使用getline和stringstream。
-
如下是一个读取的示例代码:
#include#include using namespace std; const int N = 1010; int n, m; int g[N][N]; int main() { string line; while (getline(cin, line)) { n++, m = 0; stringstream ssin(line); int x; while (ssin >> x) g[n][++m] = x; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) cout << g[i][j] << ' '; cout << endl; } return 0; }
- 测试样例:
1 2 3 4 5 6



