I noticed today that std::vector::at()
is significantly slower than accessing values with square brackets []
. According to the doc .at()
is safer because it won't let me access values beyond the bounds of the array. However, even if I access out of bound values with at()
, I'll obviously still have an error, so that's something I need to avoid no matter what.
So is there any good reason why anyone would use at()
instead of []
?
Declaration of Vectors in C++It is mandatory to include #include<vector> library before using vectors in C++.
Vector is better for frequent insertion and deletion, whereas Arrays are much better suited for frequent access of elements scenario. Vector occupies much more memory in exchange for managing storage and growing dynamically, whereas Arrays are a memory-efficient data structure.
A std::vector can never be faster than an array, as it has (a pointer to the first element of) an array as one of its data members. But the difference in run-time speed is slim and absent in any non-trivial program. One reason for this myth to persist, are examples that compare raw arrays with mis-used std::vectors.
To create a vector in C++, you first have to include the vector library. You do this by adding the line #include <vector> at the top of your file. This line goes after the line #include <iostream> and any other header files you've included in your program. The std::vector is included in the #include <vector> library.
If you have reason to believe that the index is not in your control, or if the control flow is particularly complicated and you're tracing bugs, then you might want to use at()
during the debug phase, but never inside loops or any situation where you know that the index is safe.
Even in other situations you should either prevalidate the index (e.g. if it's user input), or if you are just getting the value from a complicated algorithm, use assert
and fix the bug if there is one. [Edit.] Or perhaps if you are writing a very complicated algorithm and you aren't sure that all your indices are always valid, you could use at()
inside that algorithm and put the call into a try block -- but even here it is preferable to be offensive and use with assertions.[/]
Personally, I can't see any good reasons for at()
to survive into release code. You could possibly contrive some examples where you want to use exception handling as a convenient way to direct your control flow, but any such use case would be very situational.
The difference between at()
and the operator[]
is that at()
signals if the requested position is out of range by throwing an out_of_range exception.
So with the at()
you can react to the error state. Using the operator[] to access the vector out of index will result in undefined behavior.
at
does range check, but operator[]
does not. For example, if you pass -1 to at()
, an std::out_of_range
will be thrown. But if you do the same thing to operator[]
it will crash or strange things will happen.
If you are absolutely sure that the index is OK or you want to do the check yourself, use operator[]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With