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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2178909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:25:07  来源:igfitidea点击:

How to initialize 3D array in C++

c++carraysmultidimensional-array

提问by Chris_45

How do you initialize a 3d array in C++

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

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

回答by Carl Norum

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 个数字。

回答by Kenny Cason

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

回答by Tronic

Instead of static multidimensional arrays you should probably use one-dimensional array and calculate the index by multiplication. E.g.

您可能应该使用一维数组并通过乘法计算索引,而不是静态多维数组。例如

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. 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.

std::vector 动态分配存储空间,这是一件好事,因为堆栈空间通常非常有限,而且 3D 数组很容易占用大量空间。将它包装在这样的类中也使得将数组(通过复制或通过引用)传递给其他函数变得微不足道,而执行多维静态数组的任何传递都是非常有问题的。

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.

上面的代码只是一个例子,可以优化和完善。在各种库中肯定也有现有的实现,但我不知道。

回答by Potatoswatter

Everyone seems to forget std::valarray. It's the STL template for flat multidimensional arrays, and indexing and slicing them.

所有人似乎都忘记了std::valarray。它是用于平面多维数组以及索引和切片的 STL 模板。

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

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

No static initialization, but is that really essential?

没有静态初始化,但这真的很重要吗?