std::vector reserve / constructor question

C

Chris Roth

When I create a vector with:

vector<double> v(10);

am I right to assume that it has initialized ten elements with the
number zero?

What if I want to just make space for 10 elements I'll add in later? Is
the best way to do that:

vector<double> v;
v.reserve(10);

That is to say, there is no constructor that reserves space without
entering values (default or otherwise), right?
 
M

mlimber

When I create a vector with:

vector<double> v(10);

am I right to assume that it has initialized ten elements with the
number zero?

What if I want to just make space for 10 elements I'll add in later? Is
the best way to do that:

vector<double> v;
v.reserve(10);

That is to say, there is no constructor that reserves space without
entering values (default or otherwise), right?

Right, unfortunately.

Cheers! --M
 
M

Mark P

Chris said:
When I create a vector with:

vector<double> v(10);

am I right to assume that it has initialized ten elements with the
number zero?

Yes. The technical term here is that the elements are
"value-initialized" which, for built-in types, is the same as
zero-initialized.
What if I want to just make space for 10 elements I'll add in later? Is
the best way to do that:

vector<double> v;
v.reserve(10);

That works fine.
That is to say, there is no constructor that reserves space without
entering values (default or otherwise), right?

Yes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top