Constant-sized vector

前端 未结 7 937
無奈伤痛
無奈伤痛 2020-12-24 02:04

Does someone know the way to define constant-sized vector?

For example, instead of defining

std::vector

it will be

7条回答
  •  忘掉有多难
    2020-12-24 02:13

    The std::vector can always grow dynamically, but there are two ways you can allocate an initial size:

    This allocates initial size and fills the elements with zeroes:

    std::vector v(10);
    v.size(); //returns 10
    

    This allocates an initial size but does not populate the array with zeroes:

    std::vector v;
    v.reserve(10);
    v.size(); //returns 0
    

提交回复
热议问题