A brief analysis of the differences between string and c style strings

  • 2020-04-02 01:32:01
  • OfStack

The biggest difference is that c-style strings are static and cannot be changed dynamically.
And C++ STD ::string type dynamic management, very convenient.

A c-style string is not the same as a char array. Here are two definitions:
Char carr1 = {'a', 'b', 'c'};
Char carr2 = {'a', 'b', 'c', '\0'};
So up here, carr2 is a c-style string, carr1 is not a c-style string, and a c-style string has to end in '\0'.
The string class is a standard library class, not a built-in type. The standard library is like the class we defined ourselves. If you need to assign a value to a c-style string using the string class, you need to add '\0'.


Related articles: