Strings

hey guys would you plz tell me what is the difference between at() and these [] operators in accessing the characters of string ...which one is better and why??
http://www.cplusplus.com/reference/string/string/at/
http://www.cplusplus.com/reference/string/string/operator[]/

I think there's not much different.

(at) have "The function automatically checks whether pos is the valid position of a character in the string (i.e., whether pos is less than the string length), throwing an out_of_range exception if it is not."

So, [] will have no error if index more than length, it'll base on location in memory with undefined behavior.
But (at) doesn't.
Last edited on
Topic archived. No new replies allowed.