About comparison of C + + string and C-type character Arrays _c language

Source: Internet
Author: User

In C + +, string is a handy string that supports a variety of arithmetic and comparison operations, and is very flexible to operate. String also has the properties of a container that can be accessed by an iterator on a character element

The type C character array has the following three initialization methods:

The first two types of initialization are equivalent, and the last one has no '
*cp= ' char ' hello ';
Char c_arr[]= "Hello";
Char c_arr2[]={' h ', ' e ', ' l ', ' l ', ' o '};

These three ways of defining a C string, the first two, though different in form, have the same meaning, and there is no difference, and the length of the characters is 1 more than the actual length of the character, because both of these initializations add a ' yes ' by default at the end of the string. The third way to initialize the length is the number of characters in square brackets, which does not default to the end of the "". If you declare the same string in the first two ways in the third way, you need to declare this:

Char c_arr2[]={' h ', ' e ', ' l ', ' l ', ' o ', ' d '};

String can be initialized with a C string, as

String S1 (CP),//Copy all characters in the CP to S1 (except the end of the null character ' ")
string S2 (c_arr,2);//copy C_arr two characters to S2
string S3 (C_ARR2);// Error

Note, however, that when you initialize a string with a C string, or specify the length of the initialization (such as String S2 (c_arr,2);), if you do not specify a length, the C string must end with a ' yes ', and the program will go wrong if it does not end with '. If the code (string S3 (C_ARR2);) is compiled, an error occurs.

The string type can also be converted to a C-type string, which can be string::c_str () by a string's member method. As follows

String str ("Hello");
const char *CP=STR.C_STR ();

The above is a small series for everyone on C + + string and C-type character array of the comparison of all the content, I hope that we support the cloud-Habitat Community ~

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.