C ++ getline () uses two

getline (): read the data for an entire row. In C ++, there are two getline function. The first defined in the header <istream>, a member function of class istream; second defined in the header <string>, the ordinary function.

The first: In <istream> in getline () function has two overloaded forms:

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

Function is: read up to n characters (including end tags) s stored in the corresponding array from the istream. Not be read even if the n characters, words, or if the identifier encounters delim limit is reached, the reading is terminated. delim identifier will be read, but not be saved into an array corresponding to s. Note, delim identifier is only valid for the specified maximum number of characters n times.

#include <iostream>
using namespace std;

int main()
{
    char name[256], wolds[256];
    cout<<"Input your name: ";
    cin.getline(name,256);
    cout<<name<<endl;
    cout<<"Input your wolds: ";
    cin.getline(wolds,256,',');
    cout<<wolds<<endl;
    cin.getline(wolds,256,',');
    cout<<wolds<<endl;
    return 0;
}

Entry

Kevin
Hi,Kevin,morning

Export

Kevin
Hi
Kevin

The second: getline function <string> overload in four forms:

istream& getline (istream&  is, string& str, char delim);
istream& getline (istream&& is, string& str, char delim);
istream& getline (istream&  is, string& str);
istream& getline (istream&& is, string& str);

And a similar first usage, but as istream read parameter is passed into the function. Saves the read character string type string in str.

is: a represents the input stream, e.g. cin.

str: string of reference type for the stream storing the information in the input stream.

delim: char type variable truncation character set; without custom settings encountered '\ n', the input is terminated.

#include<iostream>
#include<string>
using namespace std;
int main(){
    string str;
    getline(cin, str, 'A');
    cout<<"The string we have gotten is :"<<str<<'.'<<endl;
    getline(cin, str, 'B');
    cout<<"The string we have gotten is :"<<str<<'.'<<endl;
return 0;}

Entry

i_am_A_student_from_Beijing

Export

The string we have gotten is :i_am_.
The string we have gotten is :_student_from_.

 

Guess you like

Origin www.cnblogs.com/yun-an/p/11458060.html