So the conclusion is: string::length() or string::size() is not obtained by calling strlen(), your experiment has proven this. String does not use 0 to determine whether the string ends. String in C++ is a class, which is very different from the simple character array in C.
If you initialize std::string with a c string and do not provide its length directly, the length can only be read as a legal c string. (There is a constructor that can provide the length)
Your second c string is illegal, and the length of this kind of string cannot be obtained. There is no way to check whether a c string is legal.
So the conclusion is: string::length() or string::size() is not obtained by calling strlen(), your experiment has proven this. String does not use 0 to determine whether the string ends. String in C++ is a class, which is very different from the simple character array in C.
If you initialize std::string with a c string and do not provide its length directly, the length can only be read as a legal c string. (There is a constructor that can provide the length)
Your second c string is illegal, and the length of this kind of string cannot be obtained. There is no way to check whether a c string is legal.