Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Initialization of vectors in c++ (http://www.velocityreviews.com/forums/t610015-initialization-of-vectors-in-c.html)

pauldepstein@att.net 04-13-2008 03:15 PM

Initialization of vectors in c++
 
Experimenting at home with visual c++, I see that int main()
{std::vector<double> vect(5);} creates a vector whose 5 initial
values are all 0. Is this standard or might the five initial values
be different from 0? I'm a bit surprised by this as I would expect
vect to consist of five uninitialized doubles. Why is it that double
x; introduces a double which is uninitialized and yet the above vect
is initialized? Or is this just a matter of the definition of the c++
language which should just be accepted, and can't be derived from some
other principle?

Thank you,

Paul Epstein

Vladislav.Lazarenko@gmail.com 04-13-2008 03:33 PM

Re: Initialization of vectors in c++
 
On Apr 13, 6:15 pm, pauldepst...@att.net wrote:
> Experimenting at home with visual c++, I see that int main()
> {std::vector<double> vect(5);} creates a vector whose 5 initial
> values are all 0. Is this standard or might the five initial values
> be different from 0? I'm a bit surprised by this as I would expect
> vect to consist of five uninitialized doubles. Why is it that double
> x; introduces a double which is uninitialized and yet the above vect
> is initialized? Or is this just a matter of the definition of the c++
> language which should just be accepted, and can't be derived from some
> other principle?
>
> Thank you,
>
> Paul Epstein


Hi. You are using constructor "vector::vector(size_type n)" that
creates a vector with n elements. Each of these elements is
initialized with default value. Constructor of template parameter of
POD type initializes your double value with 0. Try using "void
vector::reserve(size_type n)" just to reserve memory.

pauldepstein@att.net 04-13-2008 03:42 PM

Re: Initialization of vectors in c++
 
On Apr 13, 11:33*pm, Vladislav.Lazare...@gmail.com wrote:
> On Apr 13, 6:15 pm, pauldepst...@att.net wrote:
>
> > Experimenting at home with visual c++, I see that int main()
> > {std::vector<double> vect(5);} *creates a vector whose 5 initial
> > values are all 0. *Is this standard or might the five initial values
> > be different from 0? *I'm a bit surprised by this as I would expect
> > vect to consist of five uninitialized doubles. *Why is it that double
> > x; introduces a double which is uninitialized and yet the above vect
> > is initialized? *Or is this just a matter of the definition of the c++
> > language which should just be accepted, and can't be derived from some
> > other principle?

>
> > Thank you,

>
> > Paul Epstein

>
> Hi. You are using constructor "vector::vector(size_type n)" that
> creates a vector with n elements. Each of these elements is
> initialized with default value. Constructor of template parameter of
> POD type initializes your double value with 0. Try using "void
> vector::reserve(size_type n)" just to reserve memory.


Thanks, very helpful reply. Apologies for the multiple postings.

Paul

Rolf Magnus 04-13-2008 03:51 PM

Re: Initialization of vectors in c++
 
Vladislav.Lazarenko@gmail.com wrote:

> Hi. You are using constructor "vector::vector(size_type n)" that
> creates a vector with n elements. Each of these elements is
> initialized with default value.


More precisely, they are copy-initialized from a default-initialized object
of the same type.



All times are GMT. The time now is 09:14 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.