back

Syntax:

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

The back() function returns a reference to the last element in the vector. For example:

   vector<int> v;
   for( int i = 0; i < 5; i++ ) {
     v.push_back(i);
   }
   cout << "The first element is " << v.front()
        << " and the last element is " << v.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