Cstring/string/char *

Source: Internet
Author: User

Several header files that are easy to confuse

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------

<String> it is a C ++ special character container that contains the string class.
<String. h> is the character processing function set provided by standard C. For char *.
<Cstring> is provided by C ++ for compatibility with C <string. h> in the C ++ version, the main improvements include: compiling some hidden variables into the namespace; correcting some code that the C ++ compiler considers as a bug. The rest did not find many changes.

Certificate -------------------------------------------------------------------------------------------------------------------------------------------------

 

Key Point: <cstirng> is mainly used for compiling MFC, and <string> belongs to STL, both of which comply with the standard C ++, but on a non-Windows platform or Vc, use <string>. Another <string. h> is available in traditional C ++ ~

(1) Overview
String and cstring are both string template classes. string is a string class defined by the standard template class (STL) and has been incorporated into the C ++ standard;
Cstring (typedef cstringt> cstring) is the most common string class in Visual C ++. It inherits from the csimplestringt class and is mainly used in MFC and ATL programming, the main data types include char (used in ANSI), wchar_t (UNICODE), and tchar (both ANSI and Unicode );
Char * is the most commonly used string pointer in C programming. It generally ends with '/0;
(2) Construction
String is convenient and can be constructed from almost all strings, including cstring and char *;
Followed by cstring, which can be constructed from some basic string variables, including char;
Char * has no constructor and can only be assigned a value;
Example:
Char * psz = "joise ";
Cstring CSTR (psz );
String STR (CSTR );
(3) Operator Overloading
A) operator =
String is the most convenient, and almost all strings can be assigned directly, including cstring and char *;
Followed by cstring. You can directly assign values using some basic strings, including char;
Char * can only be assigned values by pointers, which is extremely dangerous. We recommend that you use strcpy or memcpy. In addition, if the initial value is not assigned to char *, we recommend that you set it to null first, to avoid wild pointers;
Example:
Char * psz = NULL;
Psz = new char [10]; // Of course, the above is directly written as 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 +
String is similar to cstring. It can be added directly to char *, but the + operator cannot be used with each other. That is, string STR = STR + CSTR is invalid and must be converted to char *;
Char * has no + operation. Only two pointers can be connected using strcat;
Example:
Char * psz = "joise ";
Cstring CSTR = psz;
CSTR = CSTR + psz;
String STR = psz;
STR = STR + psz;
Strcat (psz, psz );
Strcat (psz, CSTR); // valid
Strcat (psz, STR); // invalid. Therefore, cstring can be automatically converted to const char *, but string cannot.
C) operator ++ =
String is the most powerful and can be used with almost all string variables + =, including cstring and char *;
Followed by cstring, which can be + = with some basic string variables, including char;
Char * has no + = Operator and can only use strcat to connect two pointers;
D) operator []
The cstring is the best. When a cross-border request is made, an asserted exception is thrown;
The results of the out-of-bounds string and char * subscripts are undefined;
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> =, perator <=
Cstring and string cannot be compared, but both can be compared with char * and compared with the value rather than the address;
Cout <(psz = CSTR );
Cout <(psz = Str );
Cout <(STR = psz );
Cout <(CSTR = psz); // The code above returns 1
(4) common algorithms
A) Search
Char * string cstring
Find the specified strchr
Strstr
Strrstr
Strspfind find
The first matched value is fild_first_of findoneof.
Search for reservefind
Specify the matching method find_if

Note: In find_if, values in the range are substituted into the matching function one by one until true is returned.
B) Comparison
Char * string cstring
Find the specified value (case sensitive) strcmp
Strncmp
Strcoll
_ Strncoll operator <
Operator>
Operator <=
Operator> =
Operator =
Operator! = Collate
Compare
Search for the specified value (Case Insensitive) _ stricmp
_ Strnicmp
_ Stricoll
_ Strnicoll collatenocase
Comparenocase

Note: If the return value is <0, the previous value is smaller than the subsequent value, and vice versa.
C) replace
Char * string cstring
Find the specified value _ strset
_ Strnset replace
Replace_copy
Replace_copy_if
Replace_if
Replace

D) insert
Char * string cstring
Insert

E) add
Char * string cstring
Dynamic added value strcat push
Append append
Appendchar
Appendformat

F) intercept
Char * string cstring
Obtain the part value and use the subscript to operate substr left.
Mid
Right
Truncate

G) Remove
Char * string cstring
Remove partial values remove
Remove Blank Value removeblks
Note: This is provided by ATL. Non-C function remove_if trim
Trimleft
Trimrigth

H) case-insensitive Conversion
Char * string cstring
Convert case _ strlwr
_ Strupr makelower
Makeupper

I) conversion from other types
Char * string cstring
Convert to digital atoi
Atod
Atof format
Convert to char * c_str getbuffer
Getbuffersetlength

J) Format
Char * string cstring
Format sprintf format

K) Get the length
Char * string cstring
Get strlen length getlength
Get the size getalloclength.

L) null judgment
Char * string cstring
Judge whether it is null to determine whether = NULL or whether the first character is '/0' empty isempty

M) redefinition size
Char * string cstring
Redefinition of realloc size
New resize getbuffersetlength

N) release resources
Char * string cstring
Release free
Delete (Delete []) releasebuffer
Releasebuffersetlength

(5) Security
Cstring> string> char *
(6) Flexibility
Cstring> string> char *
(7) portability
Char * = string> cstring
Summary
To sum up, we try to use cstring in MFC and ATL. After all, it is a child of Microsoft, and each aspect is more advantageous than others, string is recommended for non-Microsoft platforms or scenarios with high portability requirements. The standard template library provides so powerful generic algorithms that you do not need to build your own wheels.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.