简体   繁体   English

初始化结构向量内的向量

[英]Initialize a vector inside a struct vector

I have a vector of structs that contain a vector, and i don't know how to initialize the fields inside the struct. 我有一个包含向量的结构向量,我不知道如何初始化结构中的字段。 I have the following code: 我有以下代码:

struct member {
  vector<pair<int, int> > rival_result;
  int matches;
}
vector<member> ranking(n);

I want to set the size of the vector inside the struct to the size of the other vector and have 0 in all the fields(matches and the integers of the pair vector). 我想将struct中的向量大小设置为另一个向量的大小,并在所有字段中匹配0(匹配和对向量的整数)。 N can be a very big number so it can't be done manually. N可以是一个非常大的数字,因此无法手动完成。 Thanks. 谢谢。

Write a constructor for member first that sets the size of your vector: 首先为member编写一个构造函数来设置向量的大小:

struct member {
    explicit member (int n) : rival_result(n), matches(0) { }
    vector<pair<int, int> > rival_result;
    int matches;
};

Now create your vector: 现在创建你的矢量:

vector<member> ranking(n, member(n));

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

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