std::vector reserve

J

john

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
 
J

john

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top