The string type treats '0' as an ordinary character, and the length is increased by one. It can be seen that '0' is not the end character and does not contain special functions. There is no need to add '0' at the end of the string
There is no string type in C language, but a character array is used instead. Generally, character arrays can be of uncertain length. char * string = "ABCD". How does the compiler know the end of the string? It usually adds '0' at the end. '
std::string and C-style string in C++ are two different strings. The former is a class defined in the standard library, and the latter is an alias for a character array.
When the C language uses a char* pointer as a string, a special character 0 is needed to mark the end position of the pointer when reading the string, which is generally considered the end of the string mark. The C++ language is object-oriented, and the length information is directly stored in the members of the object. Reading strings can be read directly based on this length, so there is no need for an end tag. Moreover, the end tag is not conducive to reading strings containing 0 characters in the string.
Picture above
The string type treats '0' as an ordinary character, and the length is increased by one. It can be seen that '0' is not the end character and does not contain special functions. There is no need to add '0' at the end of the string
There is no string type in C language, but a character array is used instead. Generally, character arrays can be of uncertain length.
char * string = "ABCD". How does the compiler know the end of the string? It usually adds '0' at the end. '
std::string
and C-style string in C++ are two different strings. The former is a class defined in the standard library, and the latter is an alias for a character array.C-style string: usually ends with
0 is the character array in C language used as the end mark. C++ strings do not have this need
Why not create a string object and print it to see its size?
When the C language uses a char* pointer as a string, a special character 0 is needed to mark the end position of the pointer when reading the string, which is generally considered the end of the string mark.
The C++ language is object-oriented, and the length information is directly stored in the members of the object. Reading strings can be read directly based on this length, so there is no need for an end tag. Moreover, the end tag is not conducive to reading strings containing 0 characters in the string.
String itself is encapsulated and will
The output is all 0x00