-2

I start to learn std::array and found function to access array with .at(index of element). Is there any reason to it over array.[index of element] style?(e.g speed, exception or other).

Jasurbek Nabijonov
  • 1,607
  • 3
  • 24
  • 37

1 Answers1

2

Consider (link)

const_reference operator[](size_type pos) const;

and (link)

const_reference at(size_type pos) const;

They both return a reference to the element at the specified location: pos. However, the former does not perform bounds checking, whereas the latter does. In case !(pos < size()) the latter will throw an std::out_of_range exception.

Jonas
  • 6,915
  • 8
  • 35
  • 53