(i) First of all, string and char*: 1, overview 2, the reciprocal transformation of the two: (ii) CString 1, overview 2, CString and the other two transformations
(a) First, string and char*:
1. Overview
In the C language:
1 |
String is a string, and char* is a character pointer. The two are not equal at any time, and when the address of the character pointer holds a string (that is, a sequence of characters at the end of ' a '), it can be said to point to a string that is not equivalent in other cases. |
In C + +: string is a character container, which is an extended template class in the template library, and char is a basic data type (the built-in type of C + +), which is even less equivalent. Where string has a constructor, Char has no constructor and can only be assigned a value (char* m= "abc"; String str (m);) 2, the reciprocal transformation of the two:
String to char*:
1 |
Use the C_str () method. ' std::string str= ' "abc"; char* p=str.c_str (); ' |
char* to String:
1 |
Use the initialization string s (char *) directly, preferably using assign () where it is not initialized; |
(ii) CString
1. Overview
CString is a very useful type of data. They largely simplify many of the operations in MFC, making it much easier for MFC to do string manipulation. One aspect of the CString type convenience feature is the connection of strings, with the CString type, you can easily connect two strings, as in the following example: