本文主要是介绍c++动态分配二维数组,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码
#include <iostream>
using namespace std;typedef int Type;
Type** create(int row, int col);
void destory(Type** p, int row);int main()
{int row, col;cin >> row >> col;Type **arr = create(row, col);for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){arr[i][j] = i * 10 + j + 1;cout << arr[i][j] << "\t";}cout << endl;}return 0;
}Type** create(int row, int col)
{if (row <= 0 || col <= 0){return NULL;}Type **p = new Type*[row];for (int i = 0; i < row; i++){p[i] = new Type[col];}return p;
}void destory(Type** p, int row)
{if (p == NULL || row <= 0){return;}for (int i = 0; i < row; i++){delete[] p[i];}delete[] p;
}
参考
c/c++动态分配
这篇关于c++动态分配二维数组的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!