Initialization of vectors in c++

Discussion in 'C++' started by pauldepstein@att.net, Apr 13, 2008.

  1. Guest

    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
     
    , Apr 13, 2008
    #1
    1. Advertising

  2. Guest

    On Apr 13, 6:15 pm, 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.
     
    , Apr 13, 2008
    #2
    1. Advertising

  3. Guest

    On Apr 13, 11:33 pm, wrote:
    > On Apr 13, 6:15 pm, 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
     
    , Apr 13, 2008
    #3
  4. Rolf Magnus Guest

    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.
     
    Rolf Magnus, Apr 13, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Denis Remezov
    Replies:
    4
    Views:
    473
    Siemel Naran
    Apr 30, 2004
  2. Replies:
    3
    Views:
    703
    Shadowman
    Mar 26, 2008
  3. Replies:
    10
    Views:
    604
    Jerry Coffin
    Apr 17, 2008
  4. Replies:
    0
    Views:
    321
  5. Guest
    Replies:
    0
    Views:
    450
    Guest
    Sep 14, 2005
Loading...

Share This Page