How can I resize a 2D C++ vector?

前端 未结 3 1171
南笙
南笙 2021-01-04 19:16

I have a 2D char vector:

vector< vector > matrix;

I will read in a matrix as an input and store it in that v

3条回答
  •  -上瘾入骨i
    2021-01-04 19:57

    While construction with sized vector as default value, given by @leemes, is quite an answer, there is an alternative without using an additional vector and copy ctor:

    assert(COL >= 0);
    assert(ROW >= 0);
    vector> matrix;
    for (size_t i{0}; i != COL; ++i)
    {
        matrix.emplace_back(ROW);
    }
    

提交回复
热议问题