New and Old code compatibility

Source: Internet
Author: User

Mix standard library class string and C-style strings

You can initialize a string class object with the literal character of strings. Typically, because C-style strings have the same data type as string literals, and are null-character-free, you can use a C-style string in any place where you can use string literals.

1. You can initialize or assign a string using the C-style string.

The addition operation of the 2.string type requires two operands, and a C-style string can be used as one of the operands, as well as a C-style string as the right-hand operand of a compound assignment operation.

Conversely, it is not valid to use the standard library string object directly where the C-style string is required. If you cannot initialize a character pointer with a string object.

char *str = ST2; Error

const char *STR = ST2.C_STR (); correctly, the C_STR function returns a C-style string that returns a pointer to the first address of the character array that holds the same content as the string object and ends with null.

C_str The returned pointer points to an array of const char types.

The array returned by C_STR is not guaranteed to be valid, and subsequent operations on ST2 may alter the value of ST2 to invalidate the array just returned. If your program needs to continuously access the data, you should copy the array returned by the C-STR function.


You cannot initialize another array directly with an array, the programmer can only create a new array, and then explicitly copy the elements of the source array to the new array individually. C + + allows you to initialize a vector object with an array, you must indicate the first element used for initialization and the address of the next location of the array with the most element.

Vector<int> Ivec (Int_arr, Int_arr + arr_size); The two pointers passed to ivec the range of vector initial values, and the second pointer points to the address space after the last element being copied.

Initialization creates a Ivec that contains arr_size elements

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.