C++三维数组定义和初始化


#include "stdio.h"
#include "vector"
int main()
{
	printf("Hello main \n");

	int Depth = 2;
	int Height = 3;
	int Width = 4;

	//================================================//
	//==================== static ====================//
	int array31[Depth][Height][Width];	//static

	int num_all3D = sizeof(array31)/sizeof(int);
	int num_all2D = sizeof(array31[0])/sizeof(int);
	int num_depth = num_all3D/num_all2D;
	int num_width = sizeof(array31[0][0])/sizeof(int);
	int num_height =  num_all2D/num_width;
	printf("num_all3D=%d  num_all2D=%d,  num_depth=%d  num_height=%d  num_width=%d\n\n",
			num_all3D, num_all2D, num_depth, num_height, num_width);

	//================================================//
	int array32[Depth][Height][Width] = {1,2,3,4,5,6};
	printf("elements in array32 (element without initialization is random):\n");
	printf("if depth>1, elements==0 (thus,we can not initialize with this method):\n");
	for(int depth=0; depth<Depth; depth++)
	{
		printf("depth=%d\n",depth);
		for(int height=0; height<Height; height++)
		{
			for(int width=0; width<Width; width++)
			{
				printf("%d ", array32[depth][height][width]);
			}
			printf("\n");
		}
		printf("\n");
	}

	//================================================//
	int array33[Depth][Height][Width] = { {1,2,3,4,5,6,7,8,9,10,11,12}, {13,14,15,16} };
	printf("elements in array33 (element without initialization is random):\n");
	for(int depth=0; depth<Depth; depth++)
	{
		printf("depth=%d\n",depth);
		for(int height=0; height<Height; height++)
		{
			for(int width=0; width<Width; width++)
			{
				printf("%d ", array33[depth][height][width]);
			}
			printf("\n");
		}
		printf("\n");
	}

	//================================================//
	int array34[Depth][Height][Width] = { {{1,2,3,4},{5,6,7,8},{9,10,11,12}}, {{4,3,2 },{8,7,6 },{12,11,10 }} };
	printf("elements in array33 (element without initialization is random):\n");
	for(int depth=0; depth<Depth; depth++)
	{
		printf("depth=%d\n",depth);
		for(int height=0; height<Height; height++)
		{
			for(int width=0; width<Width; width++)
			{
				printf("%d ", array34[depth][height][width]);
			}
			printf("\n");
		}
		printf("\n");
	}


	//=================================================//
	//==================== dynamic ====================//
	int ***array35 = new int ** [Depth];
	for(int depth=0; depth<Depth; depth++)
	{
		array35[depth] = new int*[Height];
		for(int height=0; height<Height; height++)
		{
			array35[depth][height] = new int [Width];
		}
	 }

    printf("\nAccess array35 elements:\n");
	for(int depth=0; depth<Depth; depth++)
	{
		printf("depth=%d\n",depth);
		for(int height=0; height<Height; height++)
		{
			for(int width=0; width<Width; width++)
			{
				array35[depth][height][width] = depth+height+width;
				printf("%d ", array35[depth][height][width]);
			}
			printf("\n");
		}
		printf("\n");
	}

	//  delete low-level dynamic array
	for(int depth=0; depth<Depth; depth++)
	{
		for(int height=0; height<Height; height++)
		{
			delete array35[depth][height];
		}
	 }
	//  delete high-level dynamic array
	for(int depth=0; depth<Depth; depth++)
	{
		delete array35[depth];
	 }

	//  delete higher-level dynamic array
	delete array35;


	//=================================================//
	//==================== vector ====================//
    typedef std::vector<int>  IntVector;
    typedef std::vector<IntVector>    IntVector2D;
    typedef std::vector<IntVector2D>    IntVector3D;

    IntVector3D *array36 = new IntVector3D;

    // set array size dynamically
    array36->resize(Height);
    for(int height=0; height<Height; height++)
    {
    	 (*array36)[height].resize(Width);
    	for(int width=0; width<Width; width++)
    	{
    		 (*array36)[height][width].resize(Depth);
    	}
    }

    printf("\nAccess array36 elements:\n");
	for(int depth=0; depth<Depth; depth++)
	{
		printf("depth=%d\n",depth);
		for(int height=0; height<Height; height++)
		{
			for(int width=0; width<Width; width++)
			{
				 (*array36)[depth][height][width] = depth+height+width + 10;
				printf("%d ", (*array36)[depth][height][width]);
			}
			printf("\n");
		}
		printf("\n");
	}
	delete array36;

	//================================================//
	printf("\nGoodbye main \n");

}



运行结果:

Hello main 
num_all3D=24  num_all2D=12,  num_depth=2  num_height=3  num_width=4

elements in array32 (element without initialization is random):
if depth>1, elements==0 (thus,we can not initialize with this method):
depth=0
1 2 3 4 
5 6 4096 0 
0 0 112 0 

depth=1
0 0 0 0 
0 0 0 0 
0 0 0 0 

elements in array33 (element without initialization is random):
depth=0
1 2 3 4 
5 6 7 8 
9 10 11 12 

depth=1
13 14 15 16 
2 0 -392893904 32762 
48 0 6552064 0 

elements in array33 (element without initialization is random):
depth=0
1 2 3 4 
5 6 7 8 
9 10 11 12 

depth=1
4 3 2 0 
8 7 6 32762 
12 11 10 0 


Access array35 elements:
depth=0
0 1 2 3 
1 2 3 4 
2 3 4 5 

depth=1
1 2 3 4 
2 3 4 5 
3 4 5 6 


Access array36 elements:
depth=0
10 11 12 13 
11 12 13 14 
12 13 14 15 

depth=1
11 12 13 14 
12 13 14 15 
13 14 15 16 


Goodbye main 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值