我想动态申请一个三维数组,但是一定要使得存储的空间连续的,方便管理,不知道各位有什么看法。
有个一二维的例子可以参考:
A *ga= new A [m];
ga[0] = new A [m*n];
for(int i=1;i{
ga[i]=ga[i-1]+n;
}
那么这样的内存便是一整块方便管理的了,那么三维情况又如何呢?
using namespace std;
int main()
{
int i,j,k;
int nWidth,nHeight,nThird;
cin>>nThird>>nHeight>>nWidth;
/声明一个三维数组/
int *grayScale = new int [nThird];
for (i = 0;i < nThird;i++)
{
grayScale[i] = new int*[nHeight];
for (j = 0;j < nHeight;j++)
{
grayScale[i][j] = new int [nWidth];
}
}
/将数组元素都赋值为1/
for (k = 0;k < nThird;k++)
{
for (j = 0;j < nHeight;j++)
{
for (i = 0;i < nWidth;i++)
{
grayScale[k][j][i] = 1;
}
}
}
/打印数组元素/
cout<for (k = 0;k < nThird;k++)
{
for (j = 0;j < nHeight;j++)
{
for (i = 0;i < nWidth;i++)
{
cout<<grayScale[k][j][i]<<setw(5);
}
cout<<endl;
}
cout<<endl;
}
/释放内存/
for (j = 0;j < nThird;j++)
{
for (i = 0;i < nHeight;i++)
{
delete []grayScale[j][i];
}
}
delete []grayScale;
return 0;
}
includeinclude
using namespace std;
int main()
{
int i,j,k;
int nWidth,nHeight,nThird;
cin>>nThird>>nHeight>>nWidth;
/声明一个三维数组/
int *grayScale = new int [nThird];
for (i = 0;i < nThird;i++)
{
grayScale[i] = new int*[nHeight];
for (j = 0;j < nHeight;j++)
{
grayScale[i][j] = new int [nWidth];
}
}
/将数组元素都赋值为1/
for (k = 0;k < nThird;k++)
{
for (j = 0;j < nHeight;j++)
{
for (i = 0;i < nWidth;i++)
{
grayScale[k][j][i] = 1;
}
}
}
/打印数组元素/
cout<for (k = 0;k < nThird;k++)
{
for (j = 0;j < nHeight;j++)
{
for (i = 0;i < nWidth;i++)
{
cout<<grayScale[k][j][i]<<setw(5);
}
cout<<endl;
}
cout<<endl;
}
/释放内存/
for (j = 0;j < nThird;j++)
{
for (i = 0;i < nHeight;i++)
{
delete []grayScale[j][i];
}
}
delete []grayScale;
return 0;
}
using namespace std;
int main()
{
int i,j,k;
int nWidth,nHeight,nThird;
cin>>nThird>>nHeight>>nWidth;
/声明一个三维数组/
int *grayScale = new int [nThird];
for (i = 0;i < nThird;i++)
{
grayScale[i] = new int*[nHeight];
for (j = 0;j < nHeight;j++)
{
grayScale[i][j] = new int [nWidth];
}
}
/将数组元素都赋值为1/
for (k = 0;k < nThird;k++)
{
for (j = 0;j < nHeight;j++)
{
for (i = 0;i < nWidth;i++)
{
grayScale[k][j][i] = 1;
}
}
}
/打印数组元素/
cout{
for (j = 0;j < nHeight;j++)
{
for (i = 0;i < nWidth;i++)
{
cout<<grayScale[k][j][i]<<setw(5);
}
cout<<endl;
}
cout<}
/释放内存/
for (j = 0;j < nThird;j++)
{
for (i = 0;i < nHeight;i++)
{
delete []grayScale[j][i];
}
}
delete []grayScale;
return 0;
}
//多维数组开空间
#if 1
#include
#include
void print3Dmatrix(int D3_num,int n,int m, int l) //以D3_num形式将D3_num的首地址传入
{
int i,ii,iii;
printf("\n3D matrix is: \n");
for (i=0;i<n;i++){
printf("[]\n",i);
for (ii=0;ii<m;ii++){
for (iii=0;iii<l;iii++){
printf("%.3d ",D3_num[i][ii][iii]);
}
printf("\n");
}
}
}
int **D3ToD2(int ***D3_num) //int **表示返回的是一个二级指针
{
int **D2_num;
D2_num=D3_num[0];//D3_num是一个三级指针,D3num[0]是一个二级指针,D3num[0][0]是一个一级指针
return D2_num;
}
void print2Dmatrix(int **D2_num,int m, int l)
{
int ii,iii;
printf("\n2D matrix is:\n");
for (ii=0;ii<m;ii++){
for (iii=0;iii<l;iii++){
printf("%.3d ",D2_num[ii][iii]);
}
printf("\n");
}
}
int main()
{
int i,ii,iii;//初始化参数
int n=2,m=3,l=4; //三维数组长度
int ***D3_num; //声明一个三级指针
D3_num=(int***)malloc(n*sizeof(int**)); //分配空间,该三级指针指向n个二级指针
for (i=0;i<n;i++){
D3_num[i]=(int**)malloc(m*sizeof(int*));//分配空间,每个二级指针又指向m个一级指针
for (ii=0;ii<m;ii++){
D3_num[i][ii]=(int*)malloc(l*sizeof(int));//分配空间,每个一级指针指向长度为l的一维数组
for (iii=0;iii<l;iii++){
D3_num[i][ii][iii]=(i+1)*(ii+2)*(iii+3);//为每个数组赋值,这里D3_num[i][ii][iii]的形式与我们平常用三维数组的形式一致
//但实际D3_num[i][ii][iii]是一种简写,C编译器解释起来是 *(*(*(p+i)+ii)+iii),所以可以直接赋值
}
}
}
print3Dmatrix(D3_num,n,m,l);//输出D3_num内容,这里直接将D3_num作为三级指针输入
int **D2_num; //三级指针可以表示三维数组,二级指针自然就表示二维数组了:D
D2_num=D3ToD2(D3_num);//D3ToD2返回一个二级指针,与D2_num匹配
print2Dmatrix(D2_num,m,l);
getchar();
return 0;
}
#endif
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。