This topic has not yet been rated - Rate this topic

list::back

Returns a reference to the last element of a list.

reference back( ); 
const_reference back( ) const;

The last element of the list. If the list is empty, the return value is undefined.

If the return value of back is assigned to a const_reference, the list object cannot be modified. If the return value of back 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_back.cpp
// compile with: /EHsc
#include <list>
#include <iostream>

int main( ) 
{
   using namespace std;
   list <int> c1;
   
   c1.push_back( 10 );
   c1.push_back( 11 );

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

   cout << "The last integer of c1 is " << i << endl;
   i--;
   cout << "The next-to-last integer of c1 is " << ii << endl;
}
The last integer of c1 is 11
The next-to-last integer of c1 is 10

Header: <list>

Namespace: std

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.