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).
Asked
Active
Viewed 759 times
-2

Jasurbek Nabijonov
- 1,607
- 3
- 24
- 37
-
5Did you compare the documentation for those? What did you find out? – Mat Jul 05 '17 at 07:41
-
Possible duplicate of [vector::at vs. vector::operator\[\]](https://stackoverflow.com/questions/9376049/vectorat-vs-vectoroperator) – t.niese Jul 05 '17 at 07:49
-
I found answer. With .at() it checks boundary of array. Where with [] operator it doesn't. – Jasurbek Nabijonov Jul 05 '17 at 07:52
1 Answers
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