Reputation: 5354
I often found people use the array brackets [] and a normal vector function .at (). Why are there two separate methods? What are the benefits and disadvantages of both? I know that .at () is safer, but are there any situations where .at () cannot be used? And if .at () is always safer, why ever use array brackets [].
I searched around but couldn't find a similar question. If a questions like this already exists please forward me to it and I will delete this question.
Upvotes: 41
Views: 74576
Reputation: 206616
std::vector::at()
guards you against accessing array elements out of bounds by throwing a std::out_of_range
exception unlike the []
operator which does not warn or throw exceptions when accessing beyond the vector bounds.
std::vector
is/was considered as a C++ replacement/construct for Variable Length Arrays(VLA) in c99. In order for C-style arrays to be easily replaceable by std::vector
it was necessary for vectors to provide a similar interface as that of an array, hence vector provides a []
operator for accessing its elements. At the same time, the C++ standards committee perhaps also felt the need for providing additional safety for std::vector
over C-style arrays and hence they also provided the std::vector::at()
method for this reason.
Naturally, the std::vector::at()
method checks for the size of the vector before dereferencing it and that will be a little overhead (perhaps negligible in most use cases) over accessing elements by []
, So std::vector
provides both options, to be safe or to be faster at expense of managing the safety yourself.
Upvotes: 59
Reputation: 61386
The subscript operator has less typing involved, which makes the code more clear. Also, refactoring to/from a C array comes naturally.
Upvotes: 2
Reputation: 2642
Personal Choice
The reason some people use the Subscript Operator is that they intuitive as a vector is similar to an array accessing the items this way is simply put as 'syntactic sugar' meaning it makes it look nicer.
Some people prefer the []
, and others prefer the .at()
, it's personal choice.
The Technical Choice
Assuming you're talking about access only, the Function .at()
does bounds checking and it throws an exception when you attempt to access an item beyond the bounds. The function is 'safer', but if you are handling bounds checking yourself, feel free to use the subscript operator!
So really it's up to you to choose which style of accessor you use!
Upvotes: 2
Reputation: 3538
I use STLPort 5.2.
Seems at()
does a range check.
reference at(size_type __n) { _M_range_check(__n); return (*this)[__n]; }
Upvotes: 1
Reputation: 66224
at()
Pros:
Cons:
operator[]
Pros:
Cons:
Upvotes: 14
Reputation: 12883
You're correct .at() is safer because it will check array bounds. operator[] skips the check, and is undefined if you make an illegal access.
Traditional array access in C/C++ has never had array bounds checking, and back in the 90's before Java was introduced many felt that it would add unacceptable overhead. I believe that, in general, this is not true today, and it wasn't as true as many believed at the time either. I'm sure that there are cases where it matters, but in general you're better off starting safe and switching if you find a compelling need to do so.
Upvotes: 2
Reputation: 12975
As others have mentioned, at()
performs bounds checking and []
does not. Two reasons I can think of to prefer []
are:
Upvotes: 16