简体   繁体   English

类的初始化列表中的向量

[英]Vector in the initializer list of a class

I have a learner list/vector defined in one .hpp file like this: 我在一个.hpp文件中定义了一个学习者列表/向量,如下所示:

std::vector<DecisionTree> learners_; .

Now in the constructor, this is used like this: 现在在构造函数中,它的用法如下:

Bagging::Bagging(const DataReader& dr, const int ensembleSize, uint seed) : 
  dr_(dr), 
  ensembleSize_(ensembleSize),
  learners_({}) {
  random_number_generator.seed(seed);
  buildBag();
}

What is happening in the constructor? 构造函数中发生了什么?

Does the program try to fill only two instances within that vector where the first one is a random number and second one is a call to a function? 程序是否只尝试填充该向量中的两个实例,其中第一个是随机数,第二个是对函数的调用? But how does it work because buildBag returns a decision tree but the second argument returns an integer and we declared it as a vector of decisionTrees? 但是它如何工作,因为buildBag返回一个决策树,而第二个参数返回一个整数,我们将其声明为buildBag的向量?

Why are there curly brackets ({}) ? 为什么会有大括号({})

As I have always programmed in java and python, this is hard to grasp and it does not come from any book but from the code example. 正如我一直使用Java和python编程一样,这很难理解,它不是来自任何书籍,而是来自代码示例。

dr_(dr), ensembleSize_(ensembleSize), learners_({}) : These are used to initialize the object variables of Bagging Class to the specific values provided when the constructor is called. dr_(dr), ensembleSize_(ensembleSize), learners_({}) :它们用于将Bagging Class的对象变量初始化为调用构造函数时提供的特定值。

({}) means that the std::vector<DecisionTree> learners_ is initialized to just an empty vector through an initializer list (it's a C++11 thing). ({})意味着通过初始化列表std::vector<DecisionTree> learners_初始化为一个空向量(这是C ++ 11的事情)。

{
  random_number_generator.seed(seed);
  buildBag();
}

Finally, these statements are the body of the constructor of Bagging Class. 最后,这些语句是Bagging类的构造函数的主体。 Plain commands that are executed upon creation of an object. 创建对象时执行的普通命令。

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

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