Skip to content

vector_pro::back

C++
value_type& back();

Access element

Returns a reference to the last element in the vector.

Please note that calling this function on an empty container will get an exception .

Parameters

none

Return value

A reference to the last element in the vector.

Example

C++
// vector_pro::back
#include <iostream>
#include "vector_pro.h"

/**
 * Output:
 * myvector contains: 10 9 8 7 6 5 4 3 2 1 0
 */

int main ()
{
  vector_pro<int> myvector;

  myvector.push_back(10);

  while (myvector.back() != 0)
  {
    myvector.push_back ( myvector.back() -1 );
  }

  std::cout << "myvector contains:";
  for (unsigned i=0; i<myvector.size() ; i++)
    std::cout << ' ' << myvector[i];
  std::cout << '\n';

  return 0;
}

Complexity

Constant.