Export (0) Print
Expand All

list::front

Returns a reference to the first element in a list.

reference front( ); 
const_reference front( ) const;

If the list is empty, the return is undefined.

If the return value of front is assigned to a const_reference, the list object cannot be modified. If the return value of front is assigned to a reference, the list object can be modified.

When compiling with _SECURE_SCL 1, a runtime error will occur if you attempt to access an element in an empty list. See Checked Iterators for more information.

// list_front.cpp
// compile with: /EHsc
#include <list>
#include <iostream>

int main() {
   using namespace std;
   list <int> c1;

   c1.push_back( 10 );

   int& i = c1.front();
   const int& ii = c1.front();

   cout << "The first integer of c1 is " << i << endl;
   i++;
   cout << "The first integer of c1 is " << ii << endl;
}
The first integer of c1 is 10 The first integer of c1 is 11

Header: <list>

Namespace: std

Community Additions

ADD
Show:
© 2014 Microsoft