vector::back function


The back( ) function returns the last element of the vector.

T – type of the vector.

T& back( );
const T& back( ) const ;

First function
Parameters:
none

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

Second function
Parameters:
none

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

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

Code example

vector<string> vecSt={ “Get” , ” the” , ” string” };

cout<< vecSt.back( ) ;

vecSt.back( )=” new string” ; ///changing the last string

for( auto elem:vecSt )
{
cout<< vec[0] ;
}

Output

String
Get the new string


 


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