C++建立动态二维数组
https://blog.csdn.net/longshengguoji/article/details/11131365
指针构造二维数组
构造:
int array(int m, int n) { if (m <= 0 || n <= 0) { return 0; } int** dp = new int*[m]; for (int i = 0; i < m; i++) { dp[i] = new int[n]; } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { dp[i][j] = i + 3*j; cout << dp[i][j]<<"\t"; } cout << "\n" << endl; } }
析构:
//析构 for (int i = 0; i < m; i++) { delete dp[i]; dp[i] = nullptr; }
二维vector
int _tmain(int argc, _TCHAR* argv[]) { int row,column; cin>>row>>column; //申请空间 vector<vector<int> > a(row,vector<int>(column)); //使用空间 for(int j = 0;j < row;j++) for(int k = 0;k< column;k++) a[j][k] = rand()%100; for(int j = 0;j < row;j++) { cout<<endl; for(int k = 0;k< column;k++) { a[j][k] = rand()%100; cout<<a[j][k]<<" "; } } return 0;