C-style strings, C ++ string classes, MFC, and cstring classes.

Source: Internet
Author: User

String, as its name implies, is a string composed of characters. In Standard C, standard C ++, and MFC, the implementation of the string function is different. c ++ is fully compatible with C.

1. String in Standard C

There is no data type such as string in Standard C, and the string in C is implemented through a char character array or char character pointer. For example:

Char name [26] = "this is a C-style string"; or

Char * name = "this is a C-style string ";

The type of string ends with \ 0. The memory occupied is the actual sub-character Length plus 1. the initialization and assignment values must be assigned values one by one. The modifications are not identified, and the granularity is too small and not intuitive, yesProgramStaff dispersed some high-level software issues, suchAlgorithmABSTRACT Data Type or software architecture. Char * has no constructor and can only be assigned by pointers. It is an extremely dangerous operation. If no initial value is assigned when declaring char *, we recommend that you initialize it to null first, avoid floating pointer or pointer pointing to an unknown address. Otherwise, it will be nice to get wrong!

There is no string type in Standard C, but there is string in C. h header file. Note this string. string in H is not another string, <string. h> the header file defines some frequently used function for operating strings, such as the copy function strcpy, the connection string strcat, and the comparison string strcmp, the operation objects of these functions all point to char * strings.

2. string class in Standard C ++

C ++ supports the use of C-style strings and introduces the concept of the string class. string is a string defined by STL of the standard template class and can be constructed from almost all strings.

All files of string classes are <string> and must be used with using namespace STD. Header file <string> and header file <string. h> There is no relationship. The former is the Template Library Class in the Standard C ++, and the latter is the header file in the Standard C that contains common C string processing functions, such as strcmp, the former is not an upgraded version of the latter.

It is undoubtedly correct to have a profound understanding that string is a class in Standard C ++. For example, it is defined as follows in Standard C: char * PT = NULL, however, in standard C ++, the string * PT = NULL is defined. In this way, the compiler does not have warnings or errors, but an exception occurs during running. This is because string acts as a class and calls its constructor when defining the class object. In the above example, no constructor is called and the pointer is assigned null, obviously, an error occurs. The correct method is the new operator. New in C ++ is different from malloc in C. New not only allocates a piece of memory, but also calls the class constructor. String * PT = new ("this is a C ++-style string"); or the string STR pointer is not required. The system automatically calls the default constructor to construct a string class object.

3. cstring class in MFC.

The string class in MFC is cstring, which encapsulates string and adds some interfaces. It is fully compatible with the string class, some Standard C/C ++ classes cannot be operated directly on cstring classes. cstring classes are a class in the MFC provided by Microsoft Visual C ++, therefore, only projects supporting MFC can be used. For example, in Linux, the project cannot use cstring, and only the string class in Standard C ++ can be used. In addition, because the string class is in the C ++ standard library, it is encapsulated in the STD namespace. before using it, you must declare using namespace STD; the cstring class is not in the STD namespace, because it is not a standard library of C ++, but an encapsulation library of Microsoft. In this case, the program using the string class has better portability. Cstring and string provide different interface methods, and the conversion of char * is also different. The following conversion is from csdnblog: http://blog.csdn.net/bitxinhai/article/details/2292014

4.1 cstring and String Conversion

Stringstr = "ksarea ";
Cstringcstr (Str. c_str (); // or cstring CSTR (Str. Data ();
CSTR = Str. c_str (); or CSTR = Str. Data ();
STR = CSTR. getbuffer (0); // cstring-> string
CSTR. Format ("% s", str. c_str (); // string-> cstring
CSTR. Format ("% s", str. Data (); // string-> cstring
STR = lpcstr (CSTR); // cstring-> string
/* The difference between c_str () and data () is that the former returns a string with '/0', and the latter returns a string without'/0 */

4.2.cstring and INT conversion Inti = 123;
cstringstr;
Str. format ("% d", I); // int-> other basic types of cstring conversion are similar
I = atoi (STR ); // cstring-> int and (atof, ATOL) 4.3.char * and cstring conversion cstringcstr = "ksarea ";
char * ptemp = CSTR. getbuffer (0);
char * STR;
strcpy (STR, ptemp); // cstring-> char *
CSTR. releasebuffer (-1);
char * STR = "lovesha";
cstringcstr = ST R; // char *-> the cstring type cannot be directly assigned to cstring. For conversions between int, float, string, and char *, you can use forced conversions or standard library functions. There are many conversion methods for cstring and other types, but they all share the same path. In the same direction, the type is first converted to char *, because char * is a bridge between different types. To obtain the char * type, it is very easy to convert it to another type. This Article : http://hi.baidu.com/sage_haokun/blog/item/a44b9733aa14d812ebc4afbc.html, not bad, mainly about the difference between cstring and string, interested can go to a look.

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.