C ++ string read getline function

Official 
description: ( . 1 )     
the istream & getline (the istream & IS , String & STR, char the delim); 
the istream & getline (the istream && IS , String & STR, char the delim); 
( 2 )     
the istream & getline (the istream & IS , String & STR); 
the istream & getline (the istream && IS , String & str); 

Get string from the stream line 
extracted from the character and is stored into str they until you find the delim separator character (or line feed, ( 2 ) the default ' \ n- ' ). 

IS:
istream object from which characters are extracted.
str:
string object where the extracted line is stored.
The contents in the string before the call (if any) are discarded and replaced by the extracted line.

E.g:

A first input line n, line n representative of the next input character string (character string of each line may contain spaces)

3 
'aa' aa 
in bbb 
CCC
#include <the iostream> 
#include <Vector>
 the using  namespace STD; 

int main () 
{ 
    Vector < String > VEC;
     int n-; 
    CIN >> n-; 
    . CIN GET (); // Since the carriage after the input of n, using this sentence eaten carriage return, or the following getline () Get the first string is '\ n-' 

    the while (N-- ) 
    { 
        string S; 
        getline (CIN, S); // defaults to a carriage, In terms of other symbols as delimiters, to getline (cin, s, ', '), e.g. comma 
        vec.push_back (S);        
    } 
    COUT << " Result: " <<endl;
    for(int i=0; i<vec.size(); ++i)
    {
        cout << vec.at(i) << endl;
    }

    system("pause");
    return 0;
}

 

Without cin.getr () to '\ n' eaten, the following occurs:

Will not be entered twice in the input, the output of the first row is blank (only a carriage return symbol, the display is blank)




 

Guess you like

Origin www.cnblogs.com/jodio/p/11391648.html