back

Syntax:

    #include <list>
    TYPE& back();
    const TYPE& back() const;

The back() function returns a reference to the last element in the list.

For example:

   list<int> l;
   for( int i = 0; i < 5; i++ ) {
     l.push_back(i);
   }
   cout << "The first element is " << l.front()
        << " and the last element is " << l.back() << endl;

This code produces the following output:

   The first element is 0 and the last element is 4

The back() function runs in constant time.

Related Topics: front, pop_back