Constant-sized vector

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

    This ----> std::vector<10, int> is invalid and causes error. But the new C++ standard has introduced a new class; the std::array. You can declare an array like this:

    std::array arr; // declares a new array that holds 5 ints
    std::array arr2(arr); // arr2 is equal to arr
    std::array arr3 = {1, 2, 3, 4, 5}; // arr3 holds 1, 2, 3, 4, 5
    

    The std::array has constant size and supports iterator/const_iterator/reverse_iterator/const_reverse_iterator. You can find more about this class at http://cplusplus.com/reference/stl/array/.

提交回复
热议问题