0
Suggestion
When writing a c++ peogram that contains string should I use str.at() or str[] ? I am a bit confused which one to as sometimes at() gives some error.
1 Answer
+ 5
str[] doesn't give an error for positions outside of the valid characters where at() throws an exception. If your code is deciding what positions to access, it should be save to use str[] as your testing should find any issues. If the user is saying give me position x and you verify it is within the string first, it is once more save to use str[].
On the other hand, you could ignore checking the user's position and make use of str.at() exception to handle the problem. If the exception happens, you know the user's input was bad so skip out of the code that can't be processed without having to conditionalize huge pieces of it.