vector: capacity and resize

F

Fred Zwarts

There are a few things I do not understand about the vector class
in de standard C++ library. The description of the resize member
function says:
void
resize(size_type sz);Alters the size of self. If the new size (sz) is greater than the current size, then sz-size() instances of the default value of type T are inserted at the end of the vector. If the new size is smaller than the current capacity, then the vector is truncated by erasing size()-sz elements off the end. If sz is equal to capacity then no action is taken.

Why does it compare the new size with both the current size and with the capacity?
I understood that the size and the capacity are different properties. If so, then this
description seems inconsistent to me.
Does this description mean that the capacity of a vector can be decreased by resize?
I understood that the capacity was never decreased in order to reduce the overhead
of allocating/deallocating dynamic memory.

Can somebody enlighten me?

F.Z.
 
D

Dietmar Kuehl

Fred said:
There are a few things I do not understand about the vector class
in de standard C++ library. The description of the resize member
function says:
void
resize(size_type sz);Alters the size of self. If the new size (sz) is
greater than the current size, then sz-size() instances of the default
value of type T are inserted at the end of the vector. If the new size
is smaller than the current capacity, then the vector is truncated by
erasing size()-sz elements off the end. If sz is equal to capacity then
no action is taken.

The above description is misleading: I think the "capacity" it speaks
of is actually the vector's size. The description unfortunately uses
a term also used for the size of containers (capacity) although this
term can easily be confused with an entirely different concept in the
vector class. The standard description of 'resize()' effects is rather
clear and does not use 'capacity()'.
 
P

Pete Becker

Fred said:
There are a few things I do not understand about the vector class
in de standard C++ library. The description of the resize member
function says:
void
resize(size_type sz);Alters the size of self. If the new size (sz) is greater than the current size, then sz-size() instances of the default value of type T are inserted at the end of the vector. If the new size is smaller than the current capacity, then the vector is truncated by erasing size()-sz elements off the end. If sz is equal to capacity then no action is taken.

Why does it compare the new size with both the current size and with the capacity?
I understood that the size and the capacity are different properties. If so, then this
description seems inconsistent to me.
Does this description mean that the capacity of a vector can be decreased by resize?
I understood that the capacity was never decreased in order to reduce the overhead
of allocating/deallocating dynamic memory.

Can somebody enlighten me?

It's been rewritten in the current working draft. It now reads

Effects:
if ( sz > size ())
insert (end () , sz - size () , c);
else if ( sz < size ())
erase ( begin ()+sz , end ());
else
; // do nothing
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top