简体   繁体   English

如何在C ++中初始化3D数组

[英]How to initialize 3D array in C++

如何在C ++中初始化3d数组

int min[1][1][1] = {100, { 100, {100}}}; //this is not the way

The array in your question has only one element, so you only need one value to completely initialise it. 您问题中的数组只有一个元素,因此您只需要一个值即可完全初始化它。 You need three sets of braces, one for each dimension of the array. 您需要三组大括号,一组用于阵列的每个维度。

int min[1][1][1] = {{{100}}};

A clearer example might be: 一个更清晰的例子可能是:

int arr[2][3][4] = { { {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} },
                     { {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} } };

As you can see, there are two groups, each containing three groups of 4 numbers. 如您所见,有两组,每组包含三组,每组4个。

Instead of static multidimensional arrays you should probably use one-dimensional array and calculate the index by multiplication. 您应该使用一维数组并通过乘法计算索引,而不是静态多维数组。 Eg 例如

class Array3D {
    size_t m_width, m_height;
    std::vector<int> m_data;
  public:
    Array3D(size_t x, size_t y, size_t z, int init = 0):
      m_width(x), m_height(y), m_data(x*y*z, init)
    {}
    int& operator()(size_t x, size_t y, size_t z) {
        return m_data.at(x + y * m_width + z * m_width * m_height);
    }
};

// Usage:
Array3D arr(10, 15, 20, 100); // 10x15x20 array initialized with value 100
arr(8, 12, 17) = 3;

std::vector allocates the storage dynamically, which is a good thing because the stack space is often very limited and 3D arrays easily use a lot of space. std :: vector动态分配存储,这是一件好事,因为堆栈空间通常非常有限,3D数组很容易占用大量空间。 Wrapping it in a class like that also makes passing the array (by copy or by reference) to other functions trivial, while doing any passing of multidimensional static arrays is very problematic. 将它包装在类这样的类中也会使数组(通过复制或引用)传递给其他函数是微不足道的,而进行多维静态数组的任何传递都是非常有问题的。

The above code is simply an example and it could be optimized and made more complete. 上面的代码只是一个示例,可以进行优化并使其更加完整。 There also certainly are existing implementations of this in various libraries, but I don't know of any. 当然在各种库中也存在这种实现,但我不知道。

Here's another way to dynamically allocate a 3D array in C++. 这是在C ++中动态分配3D数组的另一种方法。

int dimX = 100; int dimY = 100; int dimZ = 100;
int*** array;    // 3D array definition;
// begin memory allocation
array = new int**[dimX];
for(int x = 0; x < dimX; ++x) {
    array[x] = new int*[dimY];
    for(int y = 0; y < dimY; ++y) {
        array[x][y] = new int[dimZ];
        for(int z = 0; z < dimZ; ++z) { // initialize the values to whatever you want the default to be
            array[x][y][z] = 0;
        }
    }
}

Everyone seems to forget std::valarray . 每个人似乎都忘记了std::valarray It's the STL template for flat multidimensional arrays, and indexing and slicing them. 它是平面多维数组的STL模板,并对它们进行索引和切片。

http://www.cplusplus.com/reference/std/valarray/ http://www.cplusplus.com/reference/std/valarray/

No static initialization, but is that really essential? 没有静态初始化,但这真的很重要吗?

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

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