简体   繁体   English

包含结构的向量的向量

[英]Vector of Vectors Containing Structs

I have defined a struct 我已经定义了一个结构

struct path{
    char type;
    bool visit;
    bool inPath;
    int level;
}

I have also defined a vector of vectors of this type struct 我还定义了这种结构struct的vector的vector

vector < vector<path> > spaceStation(numLevels*levelSize,vector<path> (levelSize));

I have two questions. 我有两个问题。

1) Have i defined the vector so that the number of rows pertain to (numLevels*levelSize) and columns pertain to levelSize 1)我是否定义了向量,以便行数与(numLevels * levelSize)有关,而列与levelSize有关

2) When accessing the individual elements of the vector, how can i set the elements of the struct inside it. 2)访问向量的各个元素时,如何在其中设置结构的元素。 I have tried using the .at() member function to little success 我尝试使用.at()成员函数几乎没有成功

Re: 1 回复:1

Yes. 是。 But I can't help feeling like you wanted to do this instead: 但我不禁要感觉像您想这样做:

vector < vector<path> > spaceStation(numLevels,vector<path> (levelSize))

Note that using the term "rows" and "columns" is entirely in your imagination, concerning vectors. 请注意,关于矢量,完全在您的想象中使用术语“行”和“列”。 You just have a vector inside another vector. 您只是在另一个向量内有一个向量。 It's like an array of arrays - no special geometry implied. 这就像一个数组的数组-没有暗示特殊的几何形状。

Re: 2 回复:2

Because you have a vector of vector, you need to use two indices, not just one: 因为您有一个vector向量,所以您需要使用两个索引,而不仅仅是一个:

spaceStation[level][pathindex].visit = true;

Where spaceStation[level] returns the vector at index level , which you then take the element at position pathindex (which is an instance of your struct), and finally modify a value in that struct. 其中spaceStation[level]返回索引level的向量,然后将其放在位置pathindex (这是您的结构的实例)上的元素,最后修改该结构中的值。

For Q1, you are correct. 对于第一季度,您是正确的。 For example: a 4x4 dimension vector. 例如:4x4维向量。

vector< vector< int > > vec(4, vector(4)); vector <vector <int>> vec(4,vector(4));

For Q2, to access the path, can't you do the following: 对于Q2,要访问路径,您不能执行以下操作:

spaceStation[2][3] to access row 2 column 3 data, for example. 例如,spaceStation [2] [3]访问第2行第3列数据。

Then you can do: 然后,您可以执行以下操作:

spaceStation[2][3].visit to access elements inside your struct. spaceStation [2] [3] .visit访问结构中的元素。

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

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