简体   繁体   English

C ++向量 <vector<vector<char> &gt;&gt; myVector-如何初始化大小?

[英]C++ vector<vector<vector<char> > > myVector - How can I initialize with size?

With a 1D vector, I can use something like this: 借助一维矢量,我可以使用如下所示的内容:

vector<char> letters(5).

How can I accomplish the same thing while initializing a 3D vector? 初始化3D向量时如何完成同一件事?

Here's what I've tried: 这是我尝试过的:

vector<vector<vector<char> row(farms[0][0].size())> farm(farms[0].size())> 
   path(farms.size());

That's a bit tricky to parse, but it's just the first example nested thrice and using the same dimensions as the farms 3D vector. 解析起来有些棘手,但这只是嵌套三次的第一个示例,并且使用与Farms 3D向量相同的尺寸。 It produces these errors: 它产生以下错误:

p1.cpp:109: error: template argument 1 is invalid p1.cpp:109: error: template argument 2 is invalid p1.cpp:109: error: template argument 1 is invalid p1.cpp:109: error: template argument 2 is invalid p1.cpp:109: error: invalid type in declaration before '(' token p1.cpp:109:错误:模板参数1无效p1.cpp:109:错误:模板参数2无效p1.cpp:109:错误:模板参数1无效p1.cpp:109:错误:模板参数2无效的p1.cpp:109:错误:'('令牌之前的声明中的类型无效

vector<vector<vector<char> row(farms[0][0].size())> is clearly not a type. vector<vector<vector<char> row(farms[0][0].size())>显然不是类型。

The type you're after is the type you wrote in the title: 您想要的类型就是您在标题中写的类型:

vector<vector<vector<char> > >

Now, if you want to pre-fill each dimension so that it's a 5×5×5 vector from the outset: 现在,如果要预填充每个尺寸,以使其从一开始就是5×5×5向量:

vector<vector<vector<char> > > letters(
  5,
  vector<vector<char> >(
    5,
    vector<char>(
      5,
      '\0'
    )
  )
);

At each nested level, the first argument is the number of desired elements, and the second is the value with which to fill each of those elements. 在每个嵌套级别,第一个参数是所需元素的数量,第二个参数是用于填充每个这些元素的值。

I think it's clear that this is not good code. 我认为很明显这不是好代码。 Ask yourself again whether you really need three dimensions and, even if you do, consider a 1D vector of size 5×5×5 , with 3D indexing simulated over the top with a wrapper type. 再问一遍您是否真的需要三个维度,即使您确实需要三个维度,也可以考虑使用大小为5×5×5的一维矢量,并使用包装类型在顶部模拟3D索引。 This is only inappropriate if your matrix won't always be square, but otherwise has — among other benefits — the property of vastly reduced dynamic allocations and locality-busting indirections. 这仅在矩阵不总是正方形的情况下才是不适当的,否则矩阵具有(除了其他好处之外)具有大大减少的动态分配和破坏局部性的间接特性的优点。

I guess, if that is necessary, you'll have to initialize it by iterating over the dimensions, ie 我想,如果有必要,您必须通过遍历维度来初始化它,即

typedef std::vector<std::vector<std::vector<char > > > vector3d;

vector3d letters(5);

for(int i=0; i<letters.size(); ++i) {
    letters[i].resize(5);
    for(int j=0; j<letters[i].size(); ++j) {
        letters[i][j].resize(5);
    }
}

something like that. 这样的事情。 Please note, that did not test that code, so there might be small errors. 请注意,这没有测试该代码,因此可能会有小错误。

typedef std::vector<std::vector<std::vector<char>>> threeVec;
static constexpr unsigned int SIZE = 5;

...

threeVec tv;
tv.reserve(SIZE);
for (auto & i : tv) {
    i.reserve(SIZE);
    for (auto & j : i) {
        j.reserve(SIZE);
    }
}

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

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