vector::front function


The 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 return type of the first function allows us to change the first element of the vector.

Code example

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

cout<< vec.front( ) ;

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

cout<< vec.front( ) ;

Output

23
56

This function is apposite of the back() function.

Link:vector::back function


 


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