Constant-sized vector

前端 未结 7 925
無奈伤痛
無奈伤痛 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:24

    A std::vector is a dynamic container, there is no mechanism to restrict its growth. To allocate an initial size:

    std::vector v(10);
    

    C++11 has a std::array that would be more appropriate:

    std::array my_array;
    

    If your compiler does not support C++11 consider using boost::array:

    boost::array my_array;
    

提交回复
热议问题