vector::back
Devuelve una referencia al último elemento de vector.
reference back( );
const_reference back( ) const;
Valor devuelto
El último elemento de vector.Si el vector está vacío, el valor devuelto es indefinido.
Comentarios
Si el valor devuelto de atrás se asigna a const_reference, el objeto vector no puede modificarse.Si el valor devuelto de atrás se asigna a referencia, el objeto vector puede modificarse.
Al compilar con _SECURE_SCL 1, un error de tiempo de ejecución aparecerá si intenta tener acceso a un elemento de un vector vacío.Vea Iteradores comprobados para obtener más información.
Ejemplo
// vector_back.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
int main() {
using namespace std;
vector <int> v1;
v1.push_back( 10 );
v1.push_back( 11 );
int& i = v1.back( );
const int& ii = v1.front( );
cout << "The last integer of v1 is " << i << endl;
i--;
cout << "The next-to-last integer of v1 is "<< ii << endl;
}
Output
The last integer of v1 is 11
The next-to-last integer of v1 is 10
Requisitos
encabezado: <vector>
espacio de nombres: std