Comprehensive comparison of CString, String, Char * (1)

zhaozj2021-02-16  42

(I. Overview

String and CString are both string templates classes, String is a string class defined by the Standard Template (STL), which has been included in the C standard;

CString (Typedef CStringt > CString) is the most commonly used string class in Visual C , inherits from the CSIMpleStringt class, primarily in the MFC and ATL programming, the main data type has char (applied to ANSI), Wchar_t (unicode), Tchar (ANSI and Unicode);

Char * is the most commonly used string pointer in C program, generally in '/

0 '

End flag;

(2) Construction

2 String is convenient, can be constructed from almost all string, including cstring and char *;

2 cstring times, can be constructed from basic string variables, including char *, etc .;

2 CHAR * There is no constructor, only can be assigned;

2 Example:

Char * psz = "joise";

CSTRING CSTR (PSZ);

String STR (CSTR);

(3) Operator overload

a) Operator =

2 String is the most convenient, almost you can use all string to assign a value, including cstring and char *;

2 cstring times, you can use some basic string to assign a value, including char *, etc .;

2 CHAR * can only be assigned by the pointer, and is a very dangerous operation. It is recommended to use strcpy or memcpy, and char * If the initial value is set to NULL, it is necessary to avoid wild pointers;

2 Example:

CHAR * PSZ = NULL;

PSZ = new char [10]; // of course, the above directly writes a char * psz = new char [10];

MEMSET (PSZ, 0, 10);

STRCPY (PSZ, "JOISE");

CString CSTR;

CSTR = PSZ;

String Str;

Str = PSZ;

Str = CSTR;

Delete [] PSZ;

b) Operator

2 String is similar to cstring, you can directly add char *, but you can't use operators to use each other, that is, String Str = Str CSTR is illegal, and must be converted to char *;

2 CHAR * No operation, can only connect two pointers together with STRCAT;

2 Example:

Char * psz = "joise";

CSTRING CSTR = PSZ;

CSTR = CSTR PSZ;

String str = psz;

Str = STR STR PSZ;

STRCAT (PSZ, PSZ);

STRCAT (PSZ, CSTR); // Legal

STRCAT (PSZ, STR); // illegally, it can be seen that CString can be automatically converted to const char *, and String is not C) Operator =

2 String is the most powerful, almost can be with all string variables =, including cstring and char *;

2 cstring times, you can use the basic string variable to =, including char *, etc .;

2 CHAR * No = operator, can only connect two pointers together with STRCAT;

d) Operator []

2 cstring is best, when the cross-border will throw an assertion;

2 String and char * The subscript result is undefined;

2 Example:

Char * psz = "joise";

CSTRING CSTR = PSZ;

COUT << CSTR [8];

String str = psz;

COUT << Str [8];

Cout << PSZ [8];

e) Operator ==, operator! =, operator>, operator <, operator> =, peerator <=

2 CString and string can not be compared, but can be compared to char *, and compare values, not the address;

Cout << (PSZ == CSTR);

Cout << (PSZ == STR);

Cout << (str == psz);

COUT << (cstr == psz); // The above code is returned to 1

(4) Common algorithm

a) Find

Role Char * String CSTRING Find specified value strchr strstr strrstr strspn find find the first match value

Fild_first_of Findoneof starts from the back

RESERVEFIND specifies a matching method

Find_if

Note: Find_if is a substantial number of matching functions in the range until returns TRUE

b) comparison

Char * string CString action to find the specified value (case sensitive) strcmp strncmp strcoll _strncoll operator operator <= operator> = operator == operator! = Collate Compare to find the specified value (case insensitive) _stricmp _strnicmp _stricoll _strnicoll

CollatenOcase Comparenocase

Note: Return value If <0, the previous value is less than the value of the back, and vice versa

c) Replace

Role Char * String CSTRING Find specified value _STRSET _STRNSET REPLACE REPLACE_COPY REPLACE_COPY_IF REPLACE_IF

Replace

d) insert

Role Char * String CString Find the specified value

INSERT INSERT

Next

Joise.li

Joise@126.com

http://joise.126.com

转载请注明原文地址:https://www.9cbs.com/read-27716.html

New Post(0)