std::string my_string = ""; char test = my_string[0];
I noticed that this is not a failure, and every time I tested it, the test is 0.
Can I always depend on it 0? or is it arbitrary?
Is this bad programming?
Edit: From some comments, I understand that there are some misunderstandings regarding the usefulness of this.
The purpose of this is NOT to check if the string is empty. No need to check if the string is empty.
The situation is that there is a line that may or may not be empty. I'm only interested in the first character of this line (if it's not empty).
It seems to me that it would be less efficient to check if a string is empty, and then if it is not empty, look at the first character.
if (! my_string.empty()) test = my_string[0]; else test = 0;
Instead, I can just look at the first character without having to check if the string is empty.
test = my_string[0];
c ++ stdstring
beauxq Oct 12 '15 at 14:06 2015-10-12 14:06
source share