C++ vector::size function

The C++ vector size function returns the number of elements held by the vector currently.

size_t size( ) const ;

Parameters:
none .

Return type
size_t – A unsigned int type value which is the number of elements in the vector.

Note if the capacity is non-zero but the vector holds no element then the size is still 0.

 vector<int>vec ={ 12 , 33 , 78 } ,
vec1 ;

vec1.reserve( 3 ) ;

cout<< vec.size( ) << endl
<< vec1.size( ) ;
 

Output

3
0

Although vec1 has reserve space for three elements,it holds 0 element so,size is 0.



Leave a Reply

Your email address will not be published. Required fields are marked *