#include <stdio.h>
#include <stdlib.h>
void * malloc3d( int w, int h, int d, int size )
{
int j, k;
void ***a = (void ***) malloc( d*sizeof (void **) + h*d*sizeof (void *) + w*h*d*size );
for( k=0; k<d; k++ )
a[k] = ((void **)(a+d)) + k*h;
for( k=0; k<d; k++ )
for( j=0; j<h; j++ )
a[k][j] = ((char*)(a+d+h*d)) + (k*h+j)*w*size;
return a;
}
class Array3D
{
public:
int w, h, d;
short ***m;
Array3D( int width, int height, int depth )
{
w = width;
h = height;
d = depth;
m = (short***)malloc3d(w,h,d,sizeof (short));
}
~Array3D(){free(m);}
};
void main()
{
Array3D a(3, 4, 6);
int i, j, k;
for( k=0; k<a.d; k++ )
for( j=0; j<a.h; j++ )
for( i=0; i<a.w; i++ )
a.m[k][j][i] = i;
for( k=0; k<a.d; k++ )
{
for( j=0; j<a.h; j++ )
{
for( i=0; i<a.w; i++ )
printf( "%3i", a.m[k][j][i] );
printf( "; " );
}
printf( "/n" );
}
}
构造一个3维矩阵
最新推荐文章于 2024-07-23 11:46:27 发布