简体   繁体   English

有没有办法声明一个特定大小的 std::vector 数组?

[英]Is there a way to declare an array of std::vector of a certain size?

I want to declare an array of vectors of a certain length.我想声明一个一定长度的向量数组。 To declare an array of vectors, I write this, for instance:为了声明一个向量数组,我写了这个,例如:

std::vector<double> myarray[17][38][42];

This creates a 3-dimensional 17x38x24 array, where each element is of type std::vector<double> .这将创建一个 3 维 17x38x24 数组,其中每个元素的类型为std::vector<double>

I also know that to specify the size of a std::vector I may write, for instance:我也知道要指定我可以写的std::vector的大小,例如:

std::vector<double> myvector(14);

This creates a std::vector with space allocated for 14 elements.这将创建一个为 14 个元素分配空间的std::vector

I run into a problem when I try to combine these two procedures.当我尝试结合这两个过程时遇到了问题。 For example, writing:例如,写作:

std::vector<double> myarray[17][38][42](14);

I receive this error:我收到此错误:

error: array must be initialized with a brace-enclosed initializer [-fpermissive]错误:数组必须用大括号括起来的初始化程序初始化 [-fpermissive]
std::vector<double> myarray[17][38]42; std::vector<double> myarray[17][38]42;

When I try to initialize it this way:当我尝试以这种方式初始化它时:

std::vector<double> myotherarray(14)[17][38][42];

I receive this error:我收到此错误:

error: expected ',' or ';'错误:预期的 ',' 或 ';' before '[' token在'['标记之前
std::vector<double> myotherarray(14)[17][38][42]; std::vector<double> myotherarray(14)[17][38][42];

What is the proper way to declare every element of the array to be a std::vector of a certain size?将数组的每个元素声明为特定大小的std::vector的正确方法是什么?

You have an array of (array of array of) vectors, so if you want to inialize it, you have to use brace initializer list, and you have to initialize each vector element (effectively 38* 42 * 14 vectors.) explicitly.你有一个向量数组(array of array of),所以如果你想初始化它,你必须使用大括号初始化列表,并且你必须显式地初始化每个向量元素(实际上是 38* 42 * 14 个向量)。 This doesn't seem feasible.这似乎不可行。

If you only want to initialize the very first vector in the array (of arrays of arrays) (or a very few vectors) you can do this:如果您只想初始化数组中的第一个向量(数组的 arrays)(或很少的向量),您可以这样做:

std::vector<double> vec[17][38][42]{{{std::vector<double>(14)}}};

But from practical standpoint, you'd have to iterate over those arrays of vectors in a loop and resize them.但从实际的角度来看,您必须在循环中迭代那些向量的 arrays 并resize它们的大小。

You can't initialize such a multi-dimensional array the way you want.你不能按照你想要的方式初始化这样一个多维数组。 But you can declare it first and then use a separate initialization loop instead, eg:但是您可以先声明它,然后使用单独的初始化循环,例如:

std::vector<double> myarray[17][38][42];
for(size_t i = 0; i < 17; ++i) {
    for(size_t j = 0; j < 38; ++j) {
        for(size_t k = 0; k < 42; ++k) {
            myarray[i][j][k].resize(14);
        }
    }
}

Or:或者:

std::vector<double> myarray[17][38][42];
for(auto &d1 : myarray) {
    for(auto &d2 : d1) {
        for(auto &elem : d2) {
            elem.resize(14);
        }
    }
}

But, if you already know the vector size at compile-time, why use std::vector at all?但是,如果您在编译时已经知道向量大小,为什么还要使用std::vector呢? You can use std::array instead, eg:您可以改用std::array ,例如:

std::array<double, 14> myarray[17][38][42];

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

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