1、一维动态数组
//创建一维动态数组
int N_samples=10;
double* A = new double[N_samples];
//一维动态数组释放内存
delete[] A;
2、二维动态数组
//创建二维动态数组
int N_variables=2;//行
int N_samples=10;//列
double** ABY = new double* [N_variables];
for (int i = 0; i < N_variables; i++)
{
ABY[i] = new double[N_samples];
}
//二维动态数组释放内存
for (int i = 0; i < N_variables; i++)
{
delete[] ABY[i];
}
delete[] ABY;
3、三维动态数组
//创建三维动态数组
int height=4;//层
int N_samples=10;//行
int N_variables=10;//列
double*** ABX = new double** [height];
for (int n = 0; n < height; n++)
{
ABX[n] = new double* [N_samples];
for (int m = 0; m < N_samples; m++)
{
ABX[n][m] = new double[N_variables];
}
}
//释放三维动态数组
for (int h = 0; h < height; h++)
{
for (int i = 0; i < N_samples; i++)
{
delete[] ABX[h][i];
}
}
for (int h = 0; h < height; h++)
{
delete[] ABX[h];
}
delete[] ABX;