What is the point of declaring a vector length?

When initializing vectors, why declare their length? Is there an advantage to this?

std::vector<int> numbers(10);

Why not just

std::vector<int> numbers;

After all, aren't vectors expandable versions of arrays?
If you don't specify the size, the vector will be empty and you will have to fill it some other way e.g. using push_back, resize or insert.
Thanks.

So std::vector<int> numbers(10); is not actually empty? What 10 integers are stored in the vector then?
They will be 0 by default. You can choose another number by passing a second argument std::vector<int> numbers(10, 45); now all 10 integers will have value 45.
Last edited on
Thank you! That is clear now.
Topic archived. No new replies allowed.