简体   繁体   English

用于初始化vector的类的构造方法

[英]Class Constructor to initialize vector of vector

I want initialize a vector of vector of bool inside my costructor. 我想在构造函数内部初始化bool向量的向量。

This is my class: 这是我的课:

class MyClass{
public:
    MyClass(const OtherClass&g):
        g(g), count(g.node_count(), std::vector<bool>(16))){}


private:
    const OtherClass&g;
    std::vector<std::vector<bool>>count;
};

but when I try to initialize count I obtain this error: 但是当我尝试初始化count我得到了这个错误:

error: no match for call to ‘(std::vector<std::vector<bool> >) (int)’

You want to use fill constructor . 您要使用fill构造函数 If you don't use c++ 11 you need to specify the default value for elements in the vector count(g.node_count(), std::vector<bool>(16, true)) 如果不使用c ++ 11,则需要为向量count(g.node_count(), std::vector<bool>(16, true))元素指定默认值count(g.node_count(), std::vector<bool>(16, true))

First of all I want to ask if you know that vector<bool> is a special type of vector with a bit different implementation because of optimisation, which can cause a bit different behaviour in some cases. 首先,我想问一问您是否知道vector<bool>是一种特殊的向量类型,由于优化而在实现上略有不同,在某些情况下可能会导致行为有所不同。

If you want to use it, you must pass to constructor vector<bool>(16, true) filling it with 16 true values. 如果要使用它,则必须传递给构造函数vector<bool>(16, true) ,以16个真实值填充它。

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

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