使用向量的C ++ 3D数组声明 [英] C++ 3D array declaration using vector
问题描述
我有一些C ++代码,其中使用"vector"通过以下方法声明2D数组:
I have some C++ code where I am declaring 2D arrays using "vector" with the following method:
std::vector<std::vector<double>> Array2D(X, std::vector<double>Y);
其中X和Y是数组的尺寸.
where X and Y are the dimensions of the array.
对于我需要实现的目标,它的工作非常漂亮. 但是我想看看对3D,XYZ数组使用相同的方法. 我假设我从以下开始:
This works beautifully for what I need to achieve. However I would like to look at using the same method for 3D, XYZ arrays. I assume I start with:
std::vector<std::vector<std::vector<double>>>
但是如何声明尺寸,即Array3D(X,?????)
but how do I declare the dimensions, ie Array3D(X, ?????)
推荐答案
有 fill 向量构造函数,该构造函数构造具有n个元素的容器,每个元素都是所提供值的副本.
There is fill vector constructor, which constructs a container with n elements, and each element is a copy of value provided.
std::vector<std::vector<std::vector<double>>> Array3D(X, std::vector<std::vector<double>>(Y, std::vector<double>(Z)));
将通过Z向量创建Y乘X.您可能想对这种类型使用typedef
.
will create X by Y by Z vector. You probably would like to use typedef
for this type.
这篇关于使用向量的C ++ 3D数组声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!