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?
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?
If you want a fixed compile-time specified size (ala
std::array<T, N>
), but you want to be able to populate the vector with varying numbers of elements between0
andN
, then a good option iseastl::fixed_vector
.std::vector:
The size of a
std::vector
is dynamic - it will allocate required storage dynamically, and you cannot limit the size and enforce an error.You can however
reserve
a certain size, and then add elements up that size before it needs to allocate new storage.vector.size()
is initially 0, and increases as you add elementssstd::array:
The size of a
std::array
is a compile-time constant - it will allocate required storage statically, and you cannot change the size.array.size()
is always the size of the array, and is equal toarray.max_size()
eastl::fixed_vector:
The size of an
eastl::fixed_vector
can be either static or dynamic.It will allocate a certain number of elements initially, and then if you allow dynamic growth, will allocate dynamically if required.
For the purpose you originally asked for, you can disable growth (via
bEnableOverflow
in the template instantiation below)fixed_vector.size()
is initially 0, and increases as you add elements.Simple example:
Output:
Note that the size of
array
is 10, whereasvector
andfixed_vector
are 0