C++ getline() は、入力ストリームから文字列または行を読み取るために使用される標準ライブラリ関数です。の一部です ヘッダ 。 getline() 関数は、入力ストリームから文字を抽出し、区切り文字が見つかるまでそれを文字列オブジェクトに追加します。その際、以前に文字列オブジェクトに保存されていた値が str 入力文字列がある場合は、その文字列に置き換えられます。
getline() 関数は 2 つの方法で表現できます。
構文:
istream& getline(istream& is, string& str, char delim);>
2. パラメータ:
- は: これは istream クラスのオブジェクトであり、入力を読み取るストリームについて関数に指示します。
- 文字列: これは文字列オブジェクトであり、入力はストリームから読み取られた後にこのオブジェクトに格納されます。
- 共有: これは区切り文字で、この文字に達した後はそれ以上の入力の読み取りを停止するように関数に指示します。
例: での区切り文字の使用を示すには、 getline() 関数。
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;> }> |
>
>
出力:

出力
注記: 上記の例の場合、 #define MAX_NAME_LEN 6、 したがって、この場合、定義された制限を超えると、 、 この場合、 プログラムは実行を停止して終了します 該当する getline() 関数で使用したすべてのマクロに対して。そしてあなたはそうするでしょう 得る の 以下のように出力します。
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;> }> |
Python OSのリストディレクトリ
>
>
出力:

出力_2番目
ここでは、名前フィールドの長さが定義された制限を超えていたため、プログラムが実行を停止して終了したことが理解できます。
1. 構文:
istream& getline (istream& is, string& str);>
2. 2 番目の宣言は最初の宣言とほぼ同じです。唯一の違いは、後者には区切り文字があり、デフォルトでは改行文字(
)であることです。
パラメーター:
- は: これは istream クラスのオブジェクトであり、入力を読み取るストリームについて関数に指示します。
- 文字列: これは文字列オブジェクトであり、入力はストリームから読み取られた後にこのオブジェクトに格納されます。
以下のプログラムは getline() 関数の動作を示しています。
例 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;> }> |
>
>
入力:
Harsh Agarwal>
出力:
Hello, Harsh Agarwal welcome to GfG!>
例 2: getline() 関数を使用すると、文字に基づいて文を分割できます。それがどのように行われるかを理解するために例を見てみましょう。
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;> }> |
>
>
入力:
Hello, Faisal Al Mamun. Welcome to GfG!>
出力:
Hello, Faisal Al Mamun. Welcome to GfG!>
注意 : この関数は改行または ('
') 文字を区切り文字として考慮し、 改行文字は、この関数の有効な入力です。
新しい行がどのように問題を引き起こす可能性があるかの例を以下に示します。
例:
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;> }> |
>
>
入力:
7 MOHIT KUMAR>
出力:
Your id : 7 Hello, welcome to GfG ! Hello, MOHIT KUMAR welcome to GfG !>
関連記事:
- 入力に空白行がある場合に C++ で getline() を使用するにはどうすればよいですか?
- getline() 関数と文字配列
techcodeview.com が気に入って貢献したい場合は、次を使用して記事を書くこともできます。 または、記事を [email protected] にメールしてください。
間違っている点を見つけた場合、または上記のトピックについてさらに詳しい情報を共有したい場合は、コメントを書いてください。