What are the values of a std::vector initialized with a given size?

1.4k views Asked by At

If i initialize a std::vector like this:

vector<int> sletmig(300);

will it set all the 300 values to zero, or keep what was in my computer memory?

1

There are 1 answers

1
Cory Kramer On

They will be set to zero as the elements will be value-initialized.

From cppreference

explicit vector( size_type count );

Constructs the container with count default-inserted instances of T. No copies are made.

But know that you can also specify the default value of all the elements, for example

vector<int> sletmig(300, 0);

Again from cppreference

vector( size_type count, 
        const T& value,
        const Allocator& alloc = Allocator());

Constructs the container with count copies of elements with value value.