C++ vector > reserve size at beginning

后端 未结 4 1531
轮回少年
轮回少年 2021-01-23 21:52

in c++ I have

vector > table;

How can I resize vector so that it has 3 rows and 4 columns, all zeros?

Someth

4条回答
  •  别那么骄傲
    2021-01-23 22:31

    You can pass an explicit default value to the constructor:

    vector example(100, "example");  
    vector> table (3, vector(4));
    vector>> notveryreadable (3, vector>(4, vector (5, 999)));
    

    The last one is more readable if it's built "piecewise":

    vector dimension1(5, 999);
    vector> dimension2(4, dimension1);
    vector>> dimension3(3, dimension2);
    

    particularly if you use explicit std:: - code that looks like

    std::vector>> lol(3, std::vector>(4, std::vector (5, "lol")));
    

    should be reserved for bad jokes.

提交回复
热议问题