#include<iostream> using namespace std; int main() { //設想要創建一個rows行,cols列的矩陣 //使用new進行新建 int rows, cols; cin >> rows >> cols; int **Array = new int*[rows]; //開闢行 //new for (int i = 0; i < rows; i++) Array[i] = new int[cols]; //開闢列 for(int i = 0; i < rows; i++) for(int j = 0; j < cols; j++) cin >> Array[i][j]; for(int i = 0; i < rows; i++) { for(int j = 0;j < cols ; j++) cout << Array[i][j]; cout << endl; } //delete for(int i = 0; i < rows; i++) delete [] Array[i]; delete []Array; return 0; }
對於其它類型的也能夠寫成相似的形式,很方便。ios
參考博客:https://blog.csdn.net/u012027907/article/details/16370625spa