C++ vector::front function

The C++ vector front( ) function returns the first element of the vector.

T – type of the vector.

T& front( );
const T& front( ) const ;
First function

Parameters:
none

Return type
T& -A read and write reference to the first element.

Second function

Parameters:
none

Return type
T& -A read only reference to the first element.

The read and write reference type return by the first function allows us to change the first element of the vector.

 vector<int> vec={ 23 , 45 , 6 };

cout<< vec.front( ) ;

vec.front( )=56 ; //changing the first element

cout<< vec.front( ) ;

Guess the output!




Leave a Reply

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