How do you initialize a 3d array in C++
如何在c++中初始化一个3d数组
int min[1][1][1] = {100, { 100, {100}}}; //this is not the way
4 个解决方案
#1
59
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.
如你所见,有两个组,每个组包含3组4个数字。
#2
8
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.
上面的代码只是一个简单的示例,可以对其进行优化,使其更加完整。当然,在不同的库中也存在这样的实现,但是我不知道有什么实现。
#3
6
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;
}
}
}
#4
3
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?
没有静态初始化,但这真的是必需的吗?
#1
59
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.
如你所见,有两个组,每个组包含3组4个数字。
#2
8
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.
上面的代码只是一个简单的示例,可以对其进行优化,使其更加完整。当然,在不同的库中也存在这样的实现,但是我不知道有什么实现。
#3
6
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;
}
}
}
#4
3
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?
没有静态初始化,但这真的是必需的吗?