In C + +, string is a handy string that supports a variety of arithmetic and comparison operations, and is very flexible to operate. String also has the properties of a container that can be accessed by an iterator on a character element
The type C character array has the following three initialization methods:
The first two types of initialization are equivalent, and the last one has no '
*cp= ' char ' hello ';
Char c_arr[]= "Hello";
Char c_arr2[]={' h ', ' e ', ' l ', ' l ', ' o '};
These three ways of defining a C string, the first two, though different in form, have the same meaning, and there is no difference, and the length of the characters is 1 more than the actual length of the character, because both of these initializations add a ' yes ' by default at the end of the string. The third way to initialize the length is the number of characters in square brackets, which does not default to the end of the "". If you declare the same string in the first two ways in the third way, you need to declare this:
Char c_arr2[]={' h ', ' e ', ' l ', ' l ', ' o ', ' d '};
String can be initialized with a C string, as
String S1 (CP),//Copy all characters in the CP to S1 (except the end of the null character ' ")
string S2 (c_arr,2);//copy C_arr two characters to S2
string S3 (C_ARR2);// Error
Note, however, that when you initialize a string with a C string, or specify the length of the initialization (such as String S2 (c_arr,2);), if you do not specify a length, the C string must end with a ' yes ', and the program will go wrong if it does not end with '. If the code (string S3 (C_ARR2);) is compiled, an error occurs.
The string type can also be converted to a C-type string, which can be string::c_str () by a string's member method. As follows
String str ("Hello");
const char *CP=STR.C_STR ();
The above is a small series for everyone on C + + string and C-type character array of the comparison of all the content, I hope that we support the cloud-Habitat Community ~