Il C++ getline() è una funzione di libreria standard utilizzata per leggere una stringa o una riga da un flusso di input. Fa parte del intestazione . La funzione getline() estrae i caratteri dal flusso di input e li aggiunge all'oggetto stringa finché non viene incontrato il carattere di delimitazione. Mentre lo fai, il valore precedentemente memorizzato nell'oggetto stringa stra verrà sostituito dalla stringa di input, se presente.
La funzione getline() può essere rappresentata in due modi:
Sintassi:
istream& getline(istream& is, string& str, char delim);>
2. Parametri:
- È: È un oggetto della classe istream e indica alla funzione lo stream da cui leggere l'input.
- stringa: È un oggetto stringa, l'input viene archiviato in questo oggetto dopo essere stato letto dallo stream.
- condividere: È il carattere di delimitazione che indica alla funzione di interrompere la lettura di ulteriori input dopo aver raggiunto questo carattere.
Esempio: Per dimostrare l'uso del delimitatore nel file getline() funzione.
C++
#include> #include> using> namespace> std;> //macro definitions> #define MAX_NAME_LEN 60 // Maximum len of your name can't be more than 60> #define MAX_ADDRESS_LEN 120 // Maximum len of your address can't be more than 120> #define MAX_ABOUT_LEN 250 // Maximum len of your profession can't be more than 250> int> main () {> > char> y_name[MAX_NAME_LEN], y_address[MAX_ADDRESS_LEN], about_y[MAX_ABOUT_LEN];> > cout <<> 'Enter your name: '> ;> > cin.getline (y_name, MAX_NAME_LEN);> > cout <<> 'Enter your City: '> ;> > cin.getline (y_address, MAX_ADDRESS_LEN);> > cout <<> 'Enter your profession (press $ to complete): '> ;> > cin.getline (about_y, MAX_ABOUT_LEN,> '$'> );> //$ is a delimiter> > cout <<> '
Entered details are:
'> <<> '
'> ;> > cout <<> 'Name: '> << y_name << endl;> > cout <<> 'Address: '> << y_address << endl;> > cout <<> 'Profession is: '> << about_y << endl;> }> |
>
>
Produzione:

Produzione
Nota: Nell'esempio sopra se il #define MAX_NAME_LEN 6, Quindi in questo caso se superi il limite definito allora , in questo caso, il tuo programma interromperà l'esecuzione e uscirà applicabile per ogni macro che hai utilizzato con la funzione getline(). E lo farai Ottenere IL uscita come di seguito:
C++
#include> #include> using> namespace> std;> //macro definitions> #define MAX_NAME_LEN 6 // Maximum length of your name can't be more than 6> #define MAX_ADDRESS_LEN 120 // Maximum length of your address can't be more than 120> #define MAX_ABOUT_LEN 250 // Maximum length of your profession can't be more than 250> int> main () {> > char> y_name[MAX_NAME_LEN], y_address[MAX_ADDRESS_LEN], about_y[MAX_ABOUT_LEN];> > cout <<> 'Enter your name: '> ;> > cin.getline (y_name, MAX_NAME_LEN);> > cout <<> 'Enter your City: '> ;> > cin.getline (y_address, MAX_ADDRESS_LEN);> > cout <<> 'Enter your profession (press $ to complete): '> ;> > cin.getline (about_y, MAX_ABOUT_LEN,> '$'> );> //$ is a delimiter> > cout <<> '
Entered details are:
'> ;> > cout <<> 'Name: '> << y_name << endl;> > cout <<> 'Address: '> << y_address << endl;> > cout <<> 'Profession is: '> << about_y << endl;> }> |
>
leggi di equivalenza
>
Produzione:

Uscita_2nd
Qui è comprensibile che la lunghezza del campo del nome fosse superiore al limite definito, ecco perché il programma interrompe l'esecuzione ed esce.
1. Sintassi:
istream& getline (istream& is, string& str);>
2. La seconda dichiarazione è quasi uguale a quella della prima. L'unica differenza è che questi ultimi hanno un carattere di delimitazione che per impostazione predefinita è il carattere newline(
).
parametri:
- È: È un oggetto della classe istream e indica alla funzione lo stream da cui leggere l'input.
- stringa: È un oggetto stringa, l'input viene archiviato in questo oggetto dopo essere stato letto dallo stream.
Il programma seguente mostra il funzionamento della funzione getline()
Esempio 1:
CPP
// C++ program to demonstrate getline() function> #include> #include> using> namespace> std;> int> main()> {> > string str;> > cout <<> 'Please enter your name:
'> ;> > getline(cin, str);> > cout <<> 'Hello, '> << str> > <<> ' welcome to GfG !
'> ;> > return> 0;> }> |
>
>
Ingresso:
Harsh Agarwal>
Produzione:
convertendo una stringa fino ad oggi
Hello, Harsh Agarwal welcome to GfG!>
Esempio 2: Possiamo usare la funzione getline() per dividere una frase sulla base di un carattere. Facciamo un esempio per capire come si può fare.
CPP
// C++ program to understand the use of getline() function> #include> using> namespace> std;> int> main()> {> > string S, T;> > getline(cin, S);> > stringstream X(S);> > while> (getline(X, T,> ' '> )) {> > cout << T << endl;> > }> > return> 0;> }> |
>
>
Ingresso:
Hello, Faisal Al Mamun. Welcome to GfG!>
Produzione:
Hello, Faisal Al Mamun. Welcome to GfG!>
Attenzione : Questa funzione considera una nuova riga o un carattere ('
') come carattere di delimitazione e il carattere di nuova riga è un input valido per questa funzione.
Di seguito è riportato un esempio di come la nuova riga può causare problemi:
Esempio:
CPP
// C++ program to demonstrate> // anomaly of delimitation of> // getline() function> #include> #include> using> namespace> std;> int> main()> {> > string name;> > int> id;> > // Taking id as input> > cout <<> 'Please enter your id:
'> ;> > cin>>id;> > // Takes the empty character as input> > cout <<> 'Please enter your name:
'> ;> > getline(cin, name);> > // Prints id> > cout <<> 'Your id : '> << id <<> '
'> ;> > // Prints nothing in name field> > // as '
' is considered a valid string> > cout <<> 'Hello, '> << name> > <<> ' welcome to GfG !
'> ;> > // Again Taking string as input> > getline(cin, name);> > // This actually prints the name> > cout <<> 'Hello, '> << name> > <<> ' welcome to GfG !
'> ;> > return> 0;> }> |
>
>
Ingresso:
7 MOHIT KUMAR>
Produzione:
Your id : 7 Hello, welcome to GfG ! Hello, MOHIT KUMAR welcome to GfG !>
Articoli Correlati:
- Come utilizzare getline() in C++ quando sono presenti righe vuote nell'input?
- funzione getline() e array di caratteri
Se ti piace techcodeview.com e desideri contribuire, puoi anche scrivere un articolo utilizzando o invia il tuo articolo via email a [email protected]
Per favore scrivi commenti se trovi qualcosa di sbagliato o se desideri condividere maggiori informazioni sull'argomento discusso sopra.