Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constant-sized vector

Tags:

c++

stl

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?

like image 680
Iron-Eagle Avatar asked Jun 21 '12 08:06

Iron-Eagle


People also ask

How do I fix the size of a vector in C++?

The C++ function std::vector::resize() changes the size of vector. If n is smaller than current size then extra elements are destroyed. If n is greater than current container size then new elements are inserted at the end of vector.

What is the maximum size a vector can grow to?

max_size() is the theoretical maximum number of items that could be put in your vector. On a 32-bit system, you could in theory allocate 4Gb == 2^32 which is 2^32 char values, 2^30 int values or 2^29 double values.

Can vectors grow in size?

The capacity of the vector is completely implementation-dependent, no one can tell how it's growing..

What does vector member size () do?

size() – Returns the number of elements in the vector. max_size() – Returns the maximum number of elements that the vector can hold. capacity() – Returns the size of the storage space currently allocated to the vector expressed as number of elements.


Video Answer


1 Answers

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 
like image 152
svelten Avatar answered Oct 23 '22 08:10

svelten