C ++ Learning (1)

Source: Internet
Author: User
(1) In C ++, the function string (char *) is required to convert char * to string, for example, char * Ch = "ABC"; string STR = string (CH ); (2) if the content is not changed when char * is passed, use the const char * (3) string function to convert char * as much as possible: c_str is used to obtain the address of the string in the memory. For example, char * pS; string CH = "AAA"; PS = CH. c_str (); Note: If ch changes, PS also changes. (4) I checked it online just now. The original method (3) is not good. We should do this: char * CP = new char [edit1-> text. length () + 1];

Strcpy (CP, edit1-> text. c_str (); cause: c_str () is used to obtain the temporary address of a string. The reason is that it is a temporary address because ch is a dynamic string and the value of each string changes, the memory will be re-allocated, so the address will change dynamically. Therefore, c_str () should be out-of-the-box. Pay special attention, do not use the pointer obtained by c_str to rewrite the string value of Ch. Otherwise, you will regret it.
(5) Try not to use the pointer to control the array, because the array can save the first address information, but the pointer cannot. Especially when operating on the string, pointers are usually sorted in reverse order. (6) The string type is "an array of the appropriate number of const characters", that is, the const char [] type.

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.