Difference between Getline and get Functions

Source: Internet
Author: User

The get () function is much more flexible than the Getline function.

1. Int get () refers to extracting a single character from the stream and returning it. This is a form without parameters. Because c ++ does not use getchar ()

2. istream & get (char *, Int, char) indicates to extract characters from the stream until the terminator (\ n by default) alternatively, if the number of characters to be extracted reaches the specified value of the second parameter or the number has reached the end of the file, the characters are stored in the character array specified by the first parameter;

3. istream & get (char &) refers to extracting a single character from the stream and saving it to the reference variable;

4. istream & get (streambuf &, char) refers to storing the characters from the stream into the streambuf object until the terminator or the end of the file ..


Comparison of Getline

Istream & Getline (char *, Int, char) format.



The main difference between the two is that when the get () function encounters a qualifier, it does not read the qualifier and leaves it in the stream, while Getline () reads the qualifier, but not displayed.


#include<iostream>using namespace std;int main(){char  buf[100]={0};char sx;cin.get(buf,100,'#');cout<<buf<<endl;cin.clear();sx=cin.get();cout<<sx<<endl;return 0;}

#include<iostream>using namespace std;int main(){char  buf[100]={0};char sx;cin.getline(buf,100,'#');cout<<buf<<endl;cin.clear();sx=cin.get();cout<<sx<<endl;return 0;}


We can see that after we use get to return the stream read with get (,) and Getline (,), we find that get (,) returns the qualifier '#', getline returns the carriage return symbol after the limit. Press enter. In C language, we will use getchar () to throw the carriage return in the stream.




Difference between Getline and get Functions

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.