Getline () function in detail--from the Dusk of the Snowy summer

Source: Internet
Author: User

This article turns from the dusk of the Snowy evening, the original author Www.cnblogs.com/overcode

Students who learn C + + may encounter a getline () function, for example, in C++premer, the second section of the standard string type is "read the entire line of text with Getline." The procedure given in the book is as follows:

int main () {string Line:while (Getline (cin,line)) cout<<line<<endl; return 0;} You will find that the runtime can not jump out of the loop, or even a variety of inexplicable errors. What is this for? Here I will give you a detailed explanation. First of all to introduce you to Getline () function (personally think Baidu Encyclopedia to give the decisive not detailed) we will find Getline () of the prototype isistream& getline (IStream &is, String &str, char delim);Where IStream &is represents an input stream, for example cin;string&str means that the string read from the input stream is stored in the string (can be arbitrarily named, str or whatever); Char Delim indicates that the character is encountered to stop reading, If not set, the system defaults to the character ' \ n ', which is the carriage return newline character (encountered carriage return stop read in). Give us an example: string line;cout<< "Please cin a line:" Getline (Cin,line, ' # ');cout<<endl<< "the line you give Yes: "line; then when I type" you're the #best! ", the input stream is actually read only in the" You are the ", #后面的并没有存放到line中 (should be in the buffer bar). Then the program will run the result should be:  please cin a line:you is the #best! the line you give Is:you is the  and here to terminate subscript character as #, you enter the time even if you lose It doesn't matter if you enter a few carriage returns, the input stream will still be read, such as:                           .   Please cin a line:you is the best!//here enter a carriage return line       Thank you!      #  / /stop reading       the line give Is:you is the best! NewLine read-in and output         Thank you! above is a small example of the getline () function. So what happens if Getline () is used as a while judgment statement? Let's analyze the while (Getline (cin,line)) statement Note that the default carriage return stops reading here, press CTRL + Z or type EOF enter to exit the loop. In this statement, first getline is read from the standard input device.Into the character, and then return to the input stream CIN, note that it is CIN, so while the true judgment object of the statement is CIN, which is to determine whether there is currently a valid input stream. In this case, I think as long as your computer is not poisoned not insane how can your input stream not be effective? So in this case no matter how you enter the loop, because your input stream is valid, cannot jump out of the loop. However, some students mistakenly think that while the judgment of the sentence is line (that is, line is empty), and then want to go through the direct carriage (that is, enter an empty line) out of the loop, but found how also can not jump out of the loop. This is because your return will only terminate the read-in operation of the Getline () function. The Getline () function terminates with a while () judgment (that is, if the input stream is valid, your input stream is of course valid, satisfying the condition), so you run the Getline () function, so you understand ...

The

Getline () function is explained in detail--from the snowy summer of dusk

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.