Sintassi:
#include <vector> T& back(); const T& back() const;
La funzione back() ritorna una reference all'ultimo elemento contenuto nel vettore
Esempio:
vector<int> v; for( int i = 0; i < 5; i++ ) { v.push_back(i); } cout << "The first element is " << v.front() << " and the last element is " << v.back() << endl;
Questo codice produce il seguente output:
The first element is 0 and the last element is 4
La funzione back() gira in tempo costante.