【问题标题】:3D array C++ using int [] operator使用 int [] 运算符的 3D 数组 C++
【发布时间】:2011-04-23 16:32:38
【问题描述】:

我是 C/C++ 新手,我一直在绞尽脑汁,但仍然不知道如何制作这样的“结构”

它应该是一个使用指针的 3D 动态数组。

我是这样开始的,但是卡在那里

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

知道如何制作 y 和 z 的静态大小就足够了;

拜托,如果你能帮助我,我将不胜感激。

提前致谢。

【问题讨论】:

    标签: c++ arrays pointers multidimensional-array dynamic-arrays


    【解决方案1】:

    要动态创建 3D 整数数组,最好先了解 1D 和 2D 数组。

    一维数组:你可以很容易地做到这一点

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

    在这里,我们正在创建一个 int 指针,它将指向一块可以存储整数的内存。

    二维数组:您可以使用上述一维数组的解决方案来创建一个二维数组。首先,创建一个指针,该指针应指向一个内存块,其中仅保存其他整数指针,最终指向实际数据。由于我们的第一个指针指向一个指针数组,所以这将被称为指针到指针(双指针)。

    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*** - 因为它指向双指针)。解决方法如下:

    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;
           }
       }
    }
    

    【讨论】:

    • 您也可以将此方法用于具有 N-1 个 for 循环的 N 维数组。
    • @Manish Shukla 在 3D Array 代码中 X、Y 和 Z 中的哪个代表宽度、高度和深度??
    • @Mariya - 不要将 3 维数组视为具有宽度/高度/深度的盒子。我们构造 3D 数组的方式是,首先我们有一个简单的 1D 数组,该数组的每个元素都指向另一个 1D 数组,并且其中的每个元素再次指向将保存实际值的 1D 数组。如果您可以将其可视化,那么您将回答您的问题,如果您想将此作为宽度/高度/深度。如果您仍然需要帮助,请发表评论。
    • @ManishShukla 我是这样想的,因为我正在尝试实现一个类似于 Needlman wunsch 算法的动态规划矩阵
    • 当您停止使用嵌套数组时,不要忘记将它们向后delete[]
    【解决方案2】:
    // 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;
    

    (毕竟这是标记为 c++)

    针对乔的问题进行编辑

    你好乔=)当然。这是示例:

    #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;
    }
    

    【讨论】:

    • +1 的方式 if 这种内存布局确实是 OP 想要的
    【解决方案3】:

    你可以试试:

    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];
      }
    }
    

    一旦你使用完这个内存,你可以将它释放为:

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

    【讨论】:

      【解决方案4】:

      综合答案。

      如果你真的是用 C++(不是粗略的 C)来写这个,我认为你应该再看看这个复杂的数据结构。 IMO 重新设计,同时牢记您正在尝试做的事情会更好。

      【讨论】:

        【解决方案5】:

        您尝试做的事情在 C++ 中不是惯用的。当然,您可以为此使用int***pointer,但强烈建议不要这样做。在 C++ 中,我们有更好的方法来实现目标。

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

        这将导致内存布局类似于您所要求的。它支持动态调整大小和内部向量以具有不同的大小,就像在您的图片中一样。此外,您不必担心手动分配/删除任何一个。此外,向量知道它们的大小,因此您不必在某个地方记住它。

        但如果您只想要一个“矩形”3D 数组,其中所有元素都连续存储在同一个内存块中,您可以使用boost::multiarray

        【讨论】:

          【解决方案6】:

          好的,让我们开始吧

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

          sec 现在是一个长度为 x 的 int**s 数组,所以现在我只专注于让第零个元素成为你想要的元素

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

          现在 sec[0] 指向长度为 y 的 int*s 数组,现在只需要完成树的最后一位,所以

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

          最后把它变成图表中的形式

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

          这看起来有点像家庭作业问题,请确保您真正理解答案以及它为什么有效。

          【讨论】:

          • sec 不是数组。它是一个指向数组第一个元素的指针。
          • 这是相当语义化的。为了公平起见 int x[10]; x 实际上是指向数组的第一个元素的指针,并且两者都是数组。作为指向数组第一个元素的指针和作为数组之间确实没有区别。
          【解决方案7】:

          如果是您遇到问题的实际数组,请看这里:Declaring a pointer to multidimensional array and allocating the array

          不确定您到底想要什么,但您可能想阅读有关链表的信息。

          【讨论】:

            猜你喜欢
            • 2015-10-30
            • 2016-10-04
            • 2016-08-28
            • 1970-01-01
            • 2016-07-22
            • 1970-01-01
            • 2018-11-27
            • 2011-06-07
            • 1970-01-01
            相关资源
            最近更新 更多