logo

C++의 getline(문자열)

C++ getline() 입력 스트림에서 문자열이나 줄을 읽는 데 사용되는 표준 라이브러리 함수입니다. 그것은의 일부입니다 머리글 . getline() 함수는 입력 스트림에서 문자를 추출하여 구분 문자를 만날 때까지 문자열 객체에 추가합니다. 그렇게 하는 동안 문자열 객체에 이전에 저장된 값이 str 있는 경우 입력 문자열로 대체됩니다.
getline() 함수는 두 가지 방법으로 표현될 수 있습니다.

통사론:

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

2. 매개변수:



  • 이다: istream 클래스의 객체이며 입력을 읽을 스트림에 대한 함수를 알려줍니다.
  • str: 이는 문자열 개체이며 입력은 스트림에서 읽은 후 이 개체에 저장됩니다.
  • 공유하다: 이 문자에 도달한 후 추가 입력 읽기를 중지하도록 함수에 지시하는 구분 문자입니다.

예: 구분 기호 사용을 보여주기 위해 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;> }>

>

>

산출:

산출

메모: 위의 예에서 #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;> }>

>

>

산출:

출력_2번째

여기서 이름 필드의 길이가 정의된 제한을 초과하여 프로그램이 실행을 중지하고 종료되는 것은 이해할 수 있습니다.

1. 구문:

istream& getline (istream& is, string& str);>

2. 두 번째 선언은 첫 번째 선언과 거의 동일합니다. 유일한 차이점은 후자는 기본적으로 개행( ) 문자인 구분 문자를 갖는다는 것입니다.
매개변수:

  • 이다: istream 클래스의 객체이며 입력을 읽을 스트림에 대한 함수를 알려줍니다.
  • str: 이는 문자열 개체이며 입력은 스트림에서 읽은 후 이 개체에 저장됩니다.

아래 프로그램은 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>> 아이디;> >// 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]로 우편으로 보내세요.
잘못된 내용을 발견했거나 위에서 논의한 주제에 대해 더 많은 정보를 공유하고 싶다면 의견을 작성해 주세요.