问题描述
我有一些 C++ 代码,我在其中使用vector"和以下方法声明二维数组:
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)));
将创建 X 乘 Y 乘 Z 向量.您可能希望对这种类型使用 typedef
.
will create X by Y by Z vector. You probably would like to use typedef
for this type.
这篇关于使用向量的 C++ 3D 数组声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!