public member function

std::array::at

<array>
      reference at ( size_type n );
const_reference at ( size_type n ) const;
Access element
Returns a reference to the element at position n in the array.

The difference between this member function and member operator function operator[] is that array::at checks the array bounds and signals whether the requested position is out of range by throwing an out_of_range exception.

Parameters

n
Position of an element in the array.
If this is greater than or equal to the vector size, an exception of type out_of_range is thrown.
Notice that the first element has a position of 0, not 1.
Member type size_type is an alias of the unsigned integral type size_t.

Return value

The element at the specified position in the array.

Member types reference and const_reference are the reference types to elements of the array container.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// array::at
#include <iostream>
#include <array>

int main ()
{
  unsigned int i;
  std::array<int,10> myarray;

  // assign some values:
  for (i=0; i<10; i++) myarray.at(i) = i+1;

  // print content
  std::cout << "myarray contains:";
  for (i=0; i<10; i++)
    std::cout << " " << myarray.at(i);

  std::cout << std::endl;

  return 0;
}


Output:
myarray contains: 1 2 3 4 5 6 7 8 9 10

Complexity

Constant.

Iterator validity

No changes.

See also