[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

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

jason.cipriani@gmail.com

11/19/2008 1:25:00 AM

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
3 Answers

Joe Gottman

11/19/2008 1:48:00 AM

0

jason.cipriani@gmail.com 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

Salt_Peter

11/19/2008 2:55:00 AM

0

On Nov 18, 8:25 pm, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.com> 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.

jason.cipriani@gmail.com

11/19/2008 3:49:00 AM

0

On Nov 18, 8:48 pm, Joe Gottman <jgott...@carolina.rr.com> wrote:
> jason.cipri...@gmail.com 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