[Solved] Difference between at() and overloading operator [ ] C++ [closed]


Herb Sutter’s book exceptional c++ style covers this precise topic in item 1: at applies bound checks and will throw an exception where [] will perform undefined behavior when used out of bounds. We have freedom to choose which one we want to use in line with c++ philosophy that one should pay only for what one uses. at() is likely to be more expensive due to checks it will perform.

http://www.cplusplus.com/reference/vector/vector/at/

You can take a look at an implementation like gcc’s one for this.

3

solved Difference between at() and overloading operator [ ] C++ [closed]