mardi 18 août 2020

How to initialize vector with non ininitialization-list constructor in class member default value;

As demonstrated in code below, I want to initialize the vector member by calling constructors , I know some initilziation methods allow me to achieve this, but I wanna make my code more clear by calling them in the member declaration field.

Though it's possible, I have to write more codes. So, if there are other expressions can fit my requirement with less codes. In another word, can I ignore the constructor with the initialization-list as the paramter.

struct A {
    vector<int> vs{10, 1}; // call initializer list
};

// what i want is: 1 1 1 1 1 ...
struct B {
    vector<int> vs {vector<int>(10, 1)}; // ok, but too much codes
    vector<int> vs1 = vector<int>(10, 1);
    vector<int> vs2;
    B() : vs2(10, 1) {} // ok, but i'd like to put it in declaration
};

Aucun commentaire:

Enregistrer un commentaire