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