Search code examples
c++stlvector

Constant-sized vector


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

For example, instead of defining

std::vector<int>

it will be

std::vector<10, int>

It should be completely cross-platformed. Maybe an open source class?


Solution

  • 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<int> v(10);
    v.size(); //returns 10
    

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

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