vector::capacity function

The capacity function of vector returns the number of elements the vector can hold before it reallocates a new storage.

size_t capacity( ) const;

Whenever we add data into the vector the data is stored in the current storage of the vector.This basically means as long as the vector have enough storage the data will be added to the storage.When the current storage(discussed more about ‘current storage’ in the topic “A deeper look at capacity” below ) gets full,but if the vector must hold more data,the vector will reallocate a new storage to meet the necessary requirements.Before this reallocation takes place the number of elements the vector could contain is known as capacity of the vector.

Note after reallocation takes place the vector will have new capacity.So the capacity of vector changes with reallocation of storage.


Return type
size_t – An unsigned type value representing the number of elements the original storage of vector can hold before reallocation takes place.

Code example:

vector< int > vec(5) ; //Tells vector to allocate a storage for 5 integers

cout<<“Capacity=” << vec.capacity() << endl
<< “Size=” << vec.size() ;



The size and capacity is same here.The value 5 passed as argument to the constructor during ‘vec’ object creation,tells the vector to allocate storage big enough to hold 5 integers. And also initialize the storage with the value 0.The storage actually contain five 0 integer values so,the size is 5 and so is the capacity.

A deeper look at capacity

In creating an empty vector object the size is zero -because it does not hold any elements yet.The current storage of vector is also zero since the vector created is empty and hence it’s capacity is 0.

vector< int > vec ;

cout<< vec.size( ) << endl ///gives 0
<< vec.capacity( ) ; ///gives 0

As the program progresses suppose we start adding elements into the vector using the push_back(arg) function.The size will now increase to the number of elements added,the capacity also increases like wise.

vec.push_back(23) ; ///Adding First element
vec.push_back(34 ) ; ///Adding second element

cout<< vec.size( ) << endl ///gives 2
<< vec.capacity( ) ; ///gives 2

Let us now call the clear() function and remove all the elements in the vector.Look at the code below.

vec.clear( );

cout<< vec.size( ) << endl ///gives 0
<< vec.capacity( ) ; ///gives 2



Note the difference in the size and capacity returned by the vector.This difference in the size and capacity is sensible.The vector does not contain any element so it’ size is 0 ,but removing the element does not mean the storage of the vector will be deleted-it gets deleted only when the vector object goes out of scope.This means the vector can now hold 2 integers with it’s current storage -we like to refer to it as capacity.So we get the capacity of vector as 2.

Important note:Sometimes in calling push_back() the compiler may allocate more capacity than the required capacity.This is done to counteract the fact that more storage may be required in the future and prevent reallocation process.Reallocation takes time and also invalidate iterators ,pointers and reference -more about this is given below.


Stop wasting time,earn money($$$) from your website-Join Now!

Influence of capacity on the vector

Capacity influence vector in two ways:

i)Reallocation of vector:

When the vector capacity is not sufficient to hold the incoming elements,the vector will reallocate new storage.So reallocation takes place only when the capacity is not enough to meet the necessary requirements.

Note reallocation takes time and not only that the data should be also copied to the new storage and the side effect:it will slow down your program.Because of this it is recommended that we allocate enough capacity before we use the vector.

One way to pre-allocate capacity of specific size is to pass the capacity value during the vector object creation as argument;an instance of this is shown as the first code example of this post.Here is another example.

vector< string > vecSt , ///capacity 0
vecSt1(5) , //allocate storage to store five strings,capacity is 5

vector< char > vecC(10) ; ///capacity 10

Another way to increase the capacity is to call the function reserve( ).

vecSt.reserve (4) ;

cout<< vecSt.capacity( ) ; ///gives 4

Go to the reserve() function link to read more about the function.

ii)Reallocation invalidate iterators ,pointers and reference referring to the vector.

The iterators ,pointers and reference referring to the vector is invalidated after reallocation takes place.The reason is simple,after the reallocation the vector’s content will move to a new storage having different address value.The iterators , pointers and reference however will still be referring to the previous storage before the reallocation took place.Hence after the reallocation they will be referring to the previous storage which has now become invalid.

vector< double > vecD={ 34.56 , 98.09 };

vector< double >::iterator vecDIt=vecD.begin() ;

cout<< vecDit[0] << endl
<< vecDIt[1] ;

vecD.push_back( 78 ); ///reallocation takes place in calling push_back()

cout<< vecDit[0] << endl ///undefined
<< vecDIt[1] ; ///undefined

Output in my computer (with code:blocks)

8918832 (undefined value)
0 (undefined value)

In calling the push_back( 78 ) function the reallocation is bound to happen as the original capacity of the vector is only 2;not sufficient to hold the newly added value.