简体   繁体   English

使用int []运算符的3D数组C ++

[英]3D array C++ using int [] operator

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 我是C / C ++的新手,但我一直不知所措,但仍然不知道如何制作这样的“结构”

替代文字

It's supposed to be a 3D dynamic array using pointers. 应该是使用指针的3D动态数组。

I started like this, but got stuck there 我是这样开始的,但是被困在那里

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

It would be enough to know how to make it for a static size of y and z; 知道如何在y和z的静态大小下制作它就足够了;

Please, I'd appreciate that you help me. 拜托,谢谢您的帮助。

Thanks in advance. 提前致谢。

To create dynamically 3D array of integers, it's better you understand 1D and 2D array first. 要动态创建整数3D数组,最好先了解1D和2D数组。

1D array : You can do this very easily by 一维数组 :您可以很容易地做到这一点

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

Here, we are creating an int-pointer which will point to a chunk of memory where integers can be stored. 在这里,我们正在创建一个int指针,该指针将指向可以存储整数的一块内存。

2D array : You may use the solution of above 1D array to create a 2D array. 2D阵列 :您可以使用上述1D阵列的解决方案来创建2D阵列。 First, create a pointer which should point to a memory block where only other integer pointers are held which ultimately point to actual data. 首先,创建一个指针,该指针应指向仅保留其他整数指针的存储块,该指针最终指向实际数据。 Since our first pointer points to an array of pointers 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 Array : This is what you want to do. 3D阵列 :这就是您想要做的。 Here you may try both the scheme used in above two cases. 在这里,您可以尝试以上两种情况下使用的两种方案。 Apply the same logic as 2D array. 应用与2D数组相同的逻辑。 Diagram in question explains all. 有问题的图说明了所有内容。 The first array will be pointer-to-pointer-to-pointer (int*** - since it points to double pointers). 第一个数组将是指针到指针到指针(int ***-因为它指向双指针)。 The 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;
       }
   }
}
// one-liner
typedef std::vector<std::vector<std::vector<int> > > ThreeDimensions;
// expanded
typedef std::vector<int> OneDimension;
typedef std::vector<OneDimension> TwoDimensions;
typedef std::vector<TwoDimension> ThreeDimensions;

(this is tagged c++, after all) (毕竟这被标记为c ++)

EDIT in response to Joe's question 编辑以回应乔的问题

hello again Joe =) sure. 再次打招呼乔=)当然。 here's the example: 这是示例:

#include <vector>
#include <iostream>

int main(int argc, char* const argv[]) {

    /* one-liner */
    typedef std::vector<std::vector<std::vector<int> > >ThreeDimensions;
    /* expanded */
    typedef std::vector<int>OneDimension;
    typedef std::vector<OneDimension>TwoDimensions;
    typedef std::vector<TwoDimensions>ThreeDimensions;

    /*
       create 3 * 10 * 25 array filled with '12'
     */
    const size_t NElements1(25);
    const size_t NElements2(10);
    const size_t NElements3(3);
    const int InitialValueForAllEntries(12);

    ThreeDimensions three_dim(NElements3, TwoDimensions(NElements2, OneDimension(NElements1, InitialValueForAllEntries)));

    /* the easiest way to assign a value is to use the subscript operator */
    three_dim[0][0][0] = 11;
    /* now read the value: */
    std::cout << "It should be 11: " << three_dim[0][0][0] << "\n";
    /* every other value should be 12: */
    std::cout << "It should be 12: " << three_dim[0][1][0] << "\n";

    /* get a reference to a 2d vector: */
    TwoDimensions& two_dim(three_dim[1]);

    /* assignment */
    two_dim[2][4] = -1;
    /* read it: */
    std::cout << "It should be -1: " << two_dim[2][4] << "\n";

    /* get a reference to a 1d vector: */
    OneDimension& one_dim(two_dim[2]);

    /* read it (this is two_dim[2][4], aka three_dim[1][2][4]): */
    std::cout << "It should be -1: " << one_dim[4] << "\n";
    /* you can also use at(size_t): */
    std::cout << "It should be 12: " << one_dim.at(5) << "\n";

    return 0;
}

You can try: 你可以试试:

for(int i=0;i<x;i++) {
  sec[i] = new int *[y];
  for(int j=0;j<y;j++) {
    sec[i][j] = new int [z];
  }
}

And once you are done using this memory you can deallocate it as: 使用完该内存后,您可以将其分配为:

for(int i=0;i<x;i++) {
  for(int j=0;j<y;j++) {
    delete [] sec[i][j];
  }
  delete [] sec[i];
}
delete [] sec;

Comprehensive answers. 综合答案。

If you are really writing this in C++ (not rough C) I think you should take another look at this complicated data structure. 如果您确实是用C ++(而不是粗糙的C)编写此代码,那么我认为您应该再看一下这种复杂的数据结构。 IMO redesign while keeping in mind what you are trying to do would be better. 在记住您要做什么的同时重新设计IMO会更好。

What you're trying to do is not idiomatic in C++. 您尝试执行的操作在C ++中不是惯用的。 Of course, you can use a int***pointer for this, but this is strongly discouraged. 当然,您可以为此使用一个int***pointer ,但是强烈建议不要这样做。 In C++ we have better ways to get there. 在C ++中,我们有更好的方法可以达到目标。

vector<vector<vector<int> > > foo (5,vector<vector<int> >(4, vector<int>(3)));

This will result in something with the memory layout similar to what you asked for. 这将导致内存布局与您要求的类似。 It supports dynamic resizing and inner vectors to have different sizes just like in your picture. 它支持动态调整大小和内部矢量,使其具有与图片一样的大小。 In addition, you don't have to worry about manual allocation / deletion of any of it. 另外,您不必担心手动分配/删除其中的任何一个。 Also, the vectors know their size so you don't have to remember it somewhere. 而且,向量知道它们的大小,因此您不必在某处记住它。

But if you just want a "rectangular" 3D array where all the elements are consecutivly stored in the same memory block, you could use a boost::multiarray . 但是,如果您只需要一个“矩形” 3D数组,其中所有元素都连续存储在同一内存块中,则可以使用boost :: multiarray

OK let us take your beginnings 好,让我们开始

int ***sec = new int**[x]; 

sec is now an array of int**s of length x, so now I am just going to focus on making the zeroeth element be what you want sec现在是长度为x的int ** s的数组,所以现在我将集中讨论使zeroeth元素成为您想要的

sec[0] = new int*[y];

Now sec[0] points to array of int*s of length y, now just need to get the last bit of the tree done, so 现在sec [0]指向长度为y的int * s数组,现在只需要完成树的最后一位,所以

sec[0][0] = new int[z];

And finally to get it to the form in your diagram 最后将其放入图表中的表单

sec[0][0][z-1] = 0;

This does seem a little like a homework question, make sure you actually understand the answer and why it works. 这似乎有点像一个家庭作业问题,请确保您真正理解了答案及其起作用的原因。

If it's the actual arrays you'r having problems with look here: Declaring a pointer to multidimensional array and allocating the array 如果这是实际的数组,请在此处查看问题: 声明指向多维数组的指针并分配该数组

Not sure exactly what you want but you might want to read up on about linked lists. 不确定您到底想要什么,但您可能想阅读有关链表的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM