C + + Self-study Note _ Standard library Type _ C + + Primer

Source: Internet
Author: User

1. Standard library String type

1.1 The definition and initialization of a string object

string s1;             // default constructor, S1 is empty string string s2 (S1);         // Initialize S2 to a copy of S1 string S3 ("value");    // initializes S3 to a copy of a string literal string S4 (n,'C');      // Initialize S4 to n copies of character ' C '

1.2 The read and write of a string object

int Main () {  string  s;  Cin>>s;  cout<<s<<Endl;   return 0 ;}

Reads a string from the standard input and stores the read-in string in S. Note that the following 2 points are:

    ·  Reads and ignores all whitespace characters (such as spaces, line breaks, tabs) at the beginning;

    ·  Reads the character until it encounters a whitespace character again, and the read terminates.

So if you enter " Hello World", the result is "Hello".

1.3 Reading entire lines of text with Getline

This function accepts 2 parameters: an input stream object and a string object.

int Main () {   string line ;     while (Getline (cin,line))      cout<<line<<Endl;    return 0 ;}

It is important to note that:

    ·  Getline does not ignore the beginning of the newline character, when I come up and enter a newline character, Getline will think I have lost a line, and then stop reading and return, so if the first character is a newline character, the string parameter will be set to an empty string.

    ·  The getline encounters a newline character return, but the newline character is removed from the buffer, so the next time you read it, there is no line break.

Cond...

 

C + + Self-study Note _ Standard library Type _ C + + Primer

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.