Focus on C ++ details-standard library string type

Source: Internet
Author: User

 

1. As a standard library type, there are four basic constructor types in string. As follows:

String s; // default constructor; s is an empty string

String s (s1); // use s1 to initialize s

String s ("My Blog http://www.bkjia.com"); // initializes s to a string literal value

String s (n, 'C'); // initializes s to n 'C' copies

2. The input is mainly cin> s;

(1) read and ignore all leading blank characters.

(2) read the character until it encounters a blank character again, and the read ends.

(3) The input operator returns the read data stream.

To read an entire row, you can use the getline function. The getline function requires the first parameter to be an input stream object, and the second parameter to be a string object. It is worth noting that getline does not ignore the blank characters at the beginning of the line.

Iii. string: size_type

(1) The size () function returns the int type value directly by default. In fact, the size () function returns the string: size_type value.

(2) The string class type and many other types define some ancillary types. It is important that the use of the library type is irrelevant to the machine through this type of matching. Size_type is one of these types.

(3) the recommended string size operation result variable is of the string: size_type type. It is particularly important to try not to assign the size return value to an int variable.

4. Notes for adding string objects

When the string object and the string literal value are joined together, the left and right operands of the + operator are required.

At least one object of the string type is required.

Example. s2 = "world"; string s = "hellow" + "," + s2; this is incorrect because the first +

Both sides of the number are string literal values.

5. Convert string to char *

In many cases, we still need to convert the string type to char * for custom operations,

The C ++ standard library is also compatible with programs written in C, so the c_str () function of string can be used.

String a = "My Blog www.2cto.com ";

Char * B = a. c_str (); // This method cannot be compiled. The reason is to prevent malicious modification of the content of the string object and return a const-type object. Therefore, you must add the const modifier.

Const char * B = a. c_str (); // You can compile it.

I have briefly summarized some precautions for using string. You are welcome to join us!


 

Author Ling Feng

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.