c++ 利用new动态的定义二维数组

  • 2020-10-03
  • 程序简版
  • 公开
简介#include <iostream> using namespace std; int main() { int row = 4, col = 5; // key code: 申请空间 int **edge = (int **) new int *[row]; for (int i = 0; i < row; i++) { edge[i] = new int[col]; }
#include <iostream>
using namespace std;

int main()
{
    int row = 4, col = 5;
    // key code: 申请空间
    int **edge = (int **) new int *[row];
    for (int i = 0; i < row; i++)
    {
        edge[i] = new int[col];
    }
     
    for (int i = 0; i < row; i++)
    {
        cout << endl;
        for (int j = 0; j < col; j++)
        {
            edge[i][j] = rand()%100;
            cout << edge[i][j] << " ";
        }
    }


   //释放二维数组占用的空间
    for(int m=0;m <row;m++)
       delete[] p[m];

    return 0;

}