Skip to content

vector_pro::at

C++
value_type& at(const size_type position);

Access element

Returns a reference to the element at position in the vector.

Please note that the function automatically checks whether position is within the bounds of valid elements in the vector, and will throw an exception if it is not.

Parameters

  1. position
    Position of an element in the container.

Return value

The reference of the element at the specified position in the container.

Example

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

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

int main ()
{
  vector_pro<int> myvector(10, 0);   // 10 zero-initialized ints

  // assign some values:
  for (unsigned i=0; i<myvector.size(); i++)
    myvector.at(i)=i;

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

  return 0;
}

Complexity

Constant.