Share via


vector::front

Devuelve una referencia al primer elemento de un vector.

reference front( ); 
const_reference front( ) const;

Valor devuelto

Una referencia al primer elemento en el objeto vector.Si el vector está vacío, el valor devuelto es indefinido.

Comentarios

Si el valor devuelto de front se asigna a const_reference, el objeto vector no puede modificarse.Si el valor devuelto de front 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_front.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.front( );
   const int& ii = v1.front( );

   cout << "The first integer of v1 is "<< i << endl;
   // by incrementing i, we move the the front reference to the second element
   i++;
   cout << "Now, the first integer of v1 is "<< i << endl;
}

Output

The first integer of v1 is 10
Now, the first integer of v1 is 11

Requisitos

encabezado: <vector>

espacio de nombres: std

Vea también

Referencia

vector Class

vector::front y vector::back

Biblioteca de plantillas estándar