The C++ vector capacity function 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.
size_t – An unsigned type value representing the number of elements the original storage of vector can hold before reallocation takes place.
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.