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