3D阵列C ++使用INT []运算符 [英] 3D array C++ using int [] operator

查看:153
本文介绍了3D阵列C ++使用INT []运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C / C ++和我一直在破解我的头,但仍然有不知道如何做一个结构像这样

I'm new to C/C++ and I've been cracking my head but still got no idea how to make an "structure" like this

这应该是使用指针三维动态数组。

It's supposed to be a 3D dynamic array using pointers.

我开始喜欢这个,但卡住了有

I started like this, but got stuck there

  int x=5,y=4,z=3;
  int ***sec=new int **[x];

有将足以知道如何使它为y和z的静态大小;

It would be enough to know how to make it for a static size of y and z;

请,我AP preciate,你帮帮我吧。

Please, I'd appreciate that you help me.

先谢谢了。

推荐答案

要创建整数动态三维阵列,其更好您先了解一维和二维数组。

To create dynamically 3D array of integers, its better you understand 1D and 2D array first.

一维数组:您可以通过

const int MAX_SIZE=128;
int *arr1D = new int[MAX_SIZE];

下面,我们创建了一个INT-指针将指向一个内存块,其中整数可以存储。

Here, we are creating a int-pointer which will point to a chunk of memory where integers can be stored.

二维数组:您可以使用上述一维数组解决方案来创建二维数组。首先箱应指向一个内存块其中只有其他integeral指针是保持其最终指向实际数据的指针。因为我们的第一个指针指向指针的数组,以便将其称为为指针到指针(双指针)。

2D array: You may use solution of above 1D array to create 2D array. First crate a pointer which should point to a memory block where only other integeral pointers are hold which ultimately points to actual data. since our first pointer point to a array of pointer so this will be called as pointer-to-pointer (double pointer).

const int HEIGHT=20;
const int WIDTH=20;

int **arr2D = new int*[WIDTH];  //create an array of int pointers (int*), that will point to 
                                //data as described in 1D array.
for(int i = 0;i < WIDTH; i++){
      arr2D[i] = new int[HEIGHT]; 
}

3D阵列:这是你想要做什么。在这里,您可以尝试两种以上两种使用方案。应用相同的逻辑,二维数组。图中的问题,解释了所有。第一个数组将指针到指针到指针(INT *** - 因为它指向的双指针)。解决方法如下:

3D Array: This is what you want to do. Here you may try both the scheme used in above two. Apply the same logic as 2D array. Diagram in question explains all. First array will be pointer-to-pointer-to-pointer (int*** - since it points to double pointers). Solution is as below:

const int X=20;
const int Y=20;
const int z=20;

int ***arr3D = new int**[X];
for(int i =0; i<X; i++){
   arr3D[i] = new int*[Y];
   for(int j =0; j<Y; j++){
       arr3D[i][j] = new int[Z];
       for(int k = 0; k<Z;k++){
          arr3D[i][j][k] = 0;
       }
   }
}

这篇关于3D阵列C ++使用INT []运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆