[try Beta version]
Not logged in

 
Difference between [] operator and .at() for vectors

Jan 1, 2015 at 11:36am
can any anyone please tell me the difference between the .at() function and the [] operator in vectors.
Are they only different in context of the error generated by them?
Jan 1, 2015 at 1:02pm
operator[] does not do range checking. Accessing element not presenting in vector silently leads to undefined behavior.
.at() member function does range checking and throws an exception when you are trying to access nonexisting element.
Jan 1, 2015 at 1:34pm
Compilers often allow range checking for operator[] too but it has to be turned on. It can be useful because you can do range checking while developing your program to find errors more easily but in the final program you can turn it off to avoid the overhead.
Topic archived. No new replies allowed.