std::vector reserve

Discussion in 'C++' started by john, Mar 21, 2011.

  1. john

    john Guest

    Hi,

    If I use std::vector's reserve on a newly created vector, is the amount
    of memory reserved equal to the requested amount or at least equal to
    the requested amount. In other words, if I have


    std::vector<int> a;
    a.reserve(10);

    I know that 'a' will have memory for at least 10 int's. Can I be
    certain that the amount of memory is for exactly 10 int's?

    Thanks,
    John
    john, Mar 21, 2011
    #1
    1. Advertising

  2. john

    john Guest

    > From n3242:
    >
    > "void reserve(size_type n);
    > 2 Effects: A directive that informs a vector of a planned change in
    > size, so that it can manage the storage
    > allocation accordingly. After reserve(), capacity() is greater or equal
    > to the argument of reserve if
    > reallocation happens; and equal to the previous value of capacity()
    > otherwise. Reallocation happens
    > at this point if and only if the current capacity is less than the
    > argument of reserve(). If an exception
    > is thrown other than by the move constructor of a non-CopyInsertable
    > type, there are no effects."
    >
    > So I guess the answer is that capacity will be *at least* 10 in your
    > example.


    Thanks.
    john, Mar 21, 2011
    #2
    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. Alex Vinokur
    Replies:
    8
    Views:
    395
    Alex Vinokur
    Sep 23, 2005
  2. Dilip
    Replies:
    7
    Views:
    660
    Howard Hinnant
    Apr 22, 2006
  3. Chris Roth
    Replies:
    2
    Views:
    1,961
    Mark P
    Mar 22, 2007
  4. Mike -- Email Ignored

    std::vector: reserve required?

    Mike -- Email Ignored, Jul 4, 2008, in forum: C++
    Replies:
    22
    Views:
    1,227
    James Kanze
    Jul 10, 2008
  5. mathieu
    Replies:
    12
    Views:
    1,232
    Jeff Flinn
    May 25, 2011
Loading...

Share This Page