std::vector::assign

Discussion in 'C++' started by scooter, Sep 17, 2003.

  1. scooter

    scooter Guest

    what happens memory wise with this scenerio:

    std::vector<int> vecInt(20, 999);

    VecInt.assign(20, 0);


    do all the 999 values get overwritten or does the vector reassign the
    memory lso?

    thanks
     
    scooter, Sep 17, 2003
    #1
    1. Advertising

  2. scooter wrote:
    > what happens memory wise with this scenerio:
    >
    > std::vector<int> vecInt(20, 999);
    >
    > VecInt.assign(20, 0);


    You have the parameters backwards.

    > do all the 999 values get overwritten or does the vector reassign the
    > memory lso?


    Do you want the memory to be freed and reallocated?
    If so, I know that

    std::vector <int> vecInt (999, 20);

    {
    std::vector <int> temp ();
    temp.swap (vecInt)
    }

    will do it. IIRC, vector::assign works identically to
    constructing a new vector and using operator=, and in
    that case (again IIRC) the memory is not guaranteed to
    be released and reallocated.

    Regards,
    Buster.
     
    Buster Copley, Sep 17, 2003
    #2
    1. Advertising

  3. Buster Copley wrote:
    > scooter wrote:
    >
    >> what happens memory wise with this scenerio:
    >>
    >> std::vector<int> vecInt(20, 999);
    >>
    >> VecInt.assign(20, 0);

    >
    >
    > You have the parameters backwards.


    I'm sorry, I misunderstood the next sentence. You're fine.

    >> do all the 999 values get overwritten or does the vector reassign the
    >> memory lso?


    I think the 999s get overwritten.

    > Regards,
    > Buster.
     
    Buster Copley, Sep 17, 2003
    #3
  4. scooter

    tom_usenet Guest

    On Wed, 17 Sep 2003 20:18:39 +0100, Buster Copley <>
    wrote:

    >scooter wrote:
    >> what happens memory wise with this scenerio:
    >>
    >> std::vector<int> vecInt(20, 999);
    >>
    >> VecInt.assign(20, 0);

    >
    >You have the parameters backwards.
    >
    >> do all the 999 values get overwritten or does the vector reassign the
    >> memory lso?

    >
    >Do you want the memory to be freed and reallocated?
    >If so, I know that
    >
    >std::vector <int> vecInt (999, 20);
    >
    >{
    > std::vector <int> temp ();


    The above declares a function called temp, which isn't what you
    wanted.

    > temp.swap (vecInt)
    >}


    The canonical way to do this (which very few people seem to get right
    for some reason), is:

    std::vector<int>().swap(vecInt);

    Tom
     
    tom_usenet, Sep 18, 2003
    #4
  5. scooter

    Buster Guest

    "tom_usenet" <> wrote>

    > >Do you want the memory to be freed and reallocated?
    > >If so, I know that
    > >
    > >std::vector <int> vecInt (999, 20);
    > >
    > >{
    > > std::vector <int> temp ();

    >
    > The above declares a function called temp, which isn't what you
    > wanted.


    Yes, silly of me. Thanks.

    > > temp.swap (vecInt)
    > >}

    >
    > The canonical way to do this (which very few people seem to get right
    > for some reason), is:
    >
    > std::vector<int>().swap(vecInt);


    I have a question about that. I thought I read somewhere that a
    temporary is not modifiable. Isn't the result of the expression
    'std::vector <int> ()' (when it is an expression...) a temporary?
    So what am I missing?

    Regards,
    Buster.
     
    Buster, Sep 18, 2003
    #5
  6. scooter

    tom_usenet Guest

    On Thu, 18 Sep 2003 14:53:41 +0100, "Buster" <>
    wrote:

    >I have a question about that. I thought I read somewhere that a
    >temporary is not modifiable. Isn't the result of the expression
    >'std::vector <int> ()' (when it is an expression...) a temporary?
    >So what am I missing?


    Temporaries are modifiable, it's just that you can't bind them to
    non-const references. e.g. this is illegal:

    vecInt.swap(std::vector<int>());

    Here a temporary is passed as the argument to swap. This argument is a
    non-const reference, so the code is illegal.

    So, to summarize, you can call member functions (const or non-const)
    on non-const temporaries, but you can't bind them to non-const
    references. This is why the standard swap technique works, but some
    alternatives don't.

    Tom
     
    tom_usenet, Sep 19, 2003
    #6
    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. Anonymous
    Replies:
    20
    Views:
    4,312
    Pete Becker
    Mar 30, 2005
  2. Jason Heyes
    Replies:
    8
    Views:
    732
    Andrew Koenig
    Jan 15, 2006
  3. Replies:
    8
    Views:
    1,939
    Csaba
    Feb 18, 2006
  4. Chris Roth

    Vector Assign vs Vector operator=

    Chris Roth, Feb 21, 2007, in forum: C++
    Replies:
    4
    Views:
    370
    Ron Natalie
    Feb 22, 2007
  5. Rune Allnor
    Replies:
    4
    Views:
    956
    Rune Allnor
    Dec 11, 2008
Loading...

Share This Page