C++'s container vector
, deque
, ... provide the at(index)
accessor function in addition to operator[index]
to access container elements.
I have never, ever needed this function in my code, as it never makes sense in my C++ code to access elements that are possibly out-of-range. The code is always written to access correct indexes (or produce a meaningful error/exception in case indexes can't be made to match.)
I would be interested in real world examples (possibly from some open source project, as that would add some context) where at()
is used in production code.
Maybe someone can give an example of an algorithmic problem where using at()
made sense.
Note: I have recently used it in some unit-test code, where adding index checking code wasn't considered worth the trouble and the out_of_range exception thrown by at()
is considered enough info+context in case the test breaks.
Note: Regarding this answer by ildjarn - I do not want to start a discussion or comment war on this. I am interesting in "positive" finds, that is concrete examples where it has been used. Thank you.