CString, string, char *, compare

(I. Overview

CString string and a string are template class, a standard string string class template class (STL) defined in the C ++ standard has been included;

CString (typedef CStringT <TCHAR, StrTraitMFC <TCHAR >> CString) is most commonly used in Visual C ++ string class, the class inherits from CSimpleStringT, MFC and ATL in the main application program, the data type has mainly char (applied to the ANSI), wchar_t (unicode), TCHAR (ANSI to unicode available);

char * is the most commonly used C programming string pointer, generally '\ 0' for the end flag;

(B) construction

 string is convenient, may be constructed from almost all the strings, including CString and char *;

 CString followed, may be constructed from a number string from the basic variables, including char * and the like;

 char * has no constructor, it can only be assigned;

 For example:

char * psz = "joise";

CString cstr( psz );

string str( cstr );

(C) operator overloading

a) operator=

 string is the most convenient, almost all the strings directly assignments, including CString and char *;

 CString followed directly with some basic string assignment, including char * and the like;

 char * pointer can only be assigned by, and is a very dangerous operation, it is recommended to use strcpy or memcpy, and when it was declared char * If no initial value is recommended to set to NULL, in order to avoid dangling pointer, make you crazy;

 For example:

char *psz = NULL;

psz = new char [10]; // Of course, the above written directly char * psz = new char [10]; the same is

memset( psz, 0, 10 );

strcpy( psz, “joise” ); 

CString cstr;

cstr = psz;

string str;

str = psz;

str = cstr;

delete []psz;

b) operator+

CString string and almost directly with an addition char *, but not with each other using the + operator, i.e. string str = str + cstr is illegal, must be converted to char *;

char * + No operation, only the two pointers strcat together;

 For 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); // illegal Thus, CString be automatically converted to const char *, not the string

c) operator +=

 string is the most powerful, almost all of the + = string variables, including CString and char *;

 CString followed, some may be carried out substantially from the string variable + =, comprising char * and the like;

char * no + = operator, using only the two pointers strcat together;

d) operator[]

 CString best, when cross-border assertion will throw an exception;

 char * string with the index out of bounds undefined;

For 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> =, antagonist <=

 And it can not be compared between CString string, but can be compared with the char *, and the comparison is a value, instead of the address;

cout << ( psz == cstr );

cout << ( psz == str );

cout << ( str == psz );

cout << (cstr == psz); above are // code returns 1

(Iv) common algorithms

a) Find

Role char * string CString
 
to find the specified value strchr
strstr
strrstr
strspn the Find the Find
 
the first matching value fild_first_of FindOneOf
from behind to start looking ReserveFind
specified matching mode find_if  

NOTE: find_if the value is substituted into the range one by one until the function returns true match

b) compare


Char * string CString action
to find the specified value (case sensitive) strcmp
a strncmp
of strcoll
_strncoll operator <
operator>
operator <= 
operator> =
operator ==
operator! = The Collate

Compare 
to find the specified value (not case sensitive) _stricmp
_strnicmp
_stricoll
_strnicoll CollateNoCase

CompareNoCas 

NOTE: If the return value <0 is less than the preceding value of the back, and vice versa

c) Replace

Role char * string CString
to find the specified value _strset
_strnset
 the replace
replace_copy
replace_copy_if
replace_if Replace

d) insertion

Role char * string CString
to find the specified value insert Insert

e) increasing effect char * string CString
dynamic value added strcat push

append  Append

AppendChar

AppendFormat 


 


f) taken

Char * string CString effect
obtained by the subscript value operation part substr Left

Mid

Right

Truncate 

 

g) removed

Role char * string CString
remove a portion of the value remove Remove
to remove the blank value RemoveBlanks

Note: This is the ATL provides non-C functions remove_if Trim

TrimLeft

TrimRig 

 

h) change case

Role char * string CString
to change the case _strlwr

_strupr    MakeLower

MakeUpper 

 

i) converting other types

Role char * string CString
into digital atoi

supp

atof Format
into c_str * char
GetBuffer

GetBufferSetLen 


j) format

Role char * string CString
format sprintf Format
 

 

k) length to give

Char * string CString effect
 
obtained length strlen length GetLength
obtained size size GetAllocLength


l) is empty is determined

Char * string CString role of
determining whether is empty or == NULL is determined whether the first character is '\ 0' empty IsEmpty


m) resizing

Char * string CString effect
resizing realloc
new new Assertion in GetBufferSetLength a resize


n) the release of resources

Role char * string CString
release free

delete (delete[])    ReleaseBuffer

ReleaseBufferSetLength
 


(E) Security>

CString > string > char*;

(Vi) Flexibility

CString > string >char*;

(Vii) portability

char* = string > CString

 


f) taken

Char * string CString effect
obtained by the subscript value operation part substr Left

Mid

Right

Truncate 

 

g) removed

Role char * string CString
remove a portion of the value remove Remove
to remove the blank value RemoveBlanks

Note: This is the ATL provides non-C functions remove_if Trim

TrimLeft

TrimRig 

 

h) change case

Role char * string CString
to change the case _strlwr

_strupr    MakeLower

MakeUpper 

 

i) converting other types

Role char * string CString
into digital atoi

supp

atof Format
into c_str * char
GetBuffer

GetBufferSetLen 


j) format

Role char * string CString
format sprintf Format
 

 

k) length to give

Char * string CString effect
 
obtained length strlen length GetLength
obtained size size GetAllocLength


l) is empty is determined

Char * string CString role of
determining whether is empty == NULL is determined whether or the first character is a '\ 0' empty the IsEmpty

m) resizing

Char * string CString effect
resizing realloc
new new Assertion in GetBufferSetLength a resize


n) the release of resources

Role char * string CString
release free

delete (delete[])    ReleaseBuffer

ReleaseBufferSetLength 

(e) Security>

CString > string > char*;

(Vi) Flexibility

CString > string >char*;

(Vii) portability

char* = string > CString


 

Reproduced in: https: //www.cnblogs.com/shelvenn/archive/2008/01/11/1035184.html

Guess you like

Origin blog.csdn.net/weixin_33831196/article/details/93272913