vector::reserve() with value less than current size().

Discussion in 'C++' started by jason.cipriani@gmail.com, Nov 19, 2008.

  1. Guest

    What is the official word on what happens if I call reserve() on an
    std::vector, and specify a capacity that is less than the current
    *size* (not capacity) of that vector? Is it supposed to resize() the
    vector as well? Or just not modify anything?

    Thanks,
    Jason
     
    , Nov 19, 2008
    #1
    1. Advertising

  2. Joe Gottman Guest

    wrote:
    > What is the official word on what happens if I call reserve() on an
    > std::vector, and specify a capacity that is less than the current
    > *size* (not capacity) of that vector? Is it supposed to resize() the
    > vector as well? Or just not modify anything?
    >
    > Thanks,
    > Jason


    reserve() never reduces capacity; it just increases it. Therefore this
    is a no-op.

    Joe Gottman
     
    Joe Gottman, Nov 19, 2008
    #2
    1. Advertising

  3. Salt_Peter Guest

    On Nov 18, 8:25 pm, ""
    <> wrote:
    > What is the official word on what happens if I call reserve() on an
    > std::vector, and specify a capacity that is less than the current
    > *size* (not capacity) of that vector? Is it supposed to resize() the
    > vector as well? Or just not modify anything?
    >
    > Thanks,
    > Jason


    The size of the vector is irrelevant. If reserve's arguement is less
    than or equal to its capacity, nothing happens. Swap the vector to
    (maybe) change its capacity.
     
    Salt_Peter, Nov 19, 2008
    #3
  4. Guest

    On Nov 18, 8:48 pm, Joe Gottman <> wrote:
    > wrote:
    > > What is the official word on what happens if I call reserve() on an
    > > std::vector, and specify a capacity that is less than the current
    > > *size* (not capacity) of that vector? Is it supposed to resize() the
    > > vector as well? Or just not modify anything?

    >
    > > Thanks,
    > > Jason

    >
    > reserve() never reduces capacity; it just increases it.  Therefore this
    > is a no-op.


    That's what I was looking for, thanks Joe and Salt_Peter.

    Also, sorry, I'm looking now and it turns out that actually *is*
    stated in the docs I was reading, it's just in a footnote that I
    missed. :-o

    Jason
     
    , Nov 19, 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. falcon
    Replies:
    10
    Views:
    19,256
    Roedy Green
    Feb 24, 2006
  2. Replies:
    8
    Views:
    2,003
    Csaba
    Feb 18, 2006
  3. Graeter than or less than

    , Jun 21, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    889
    bruce barker
    Jun 21, 2007
  4. Diwa
    Replies:
    7
    Views:
    1,073
  5. Dwight Army of Champions
    Replies:
    4
    Views:
    2,903
    John H.
    Mar 17, 2010
Loading...

Share This Page