다음을 통해 공유


getline 템플릿 함수

입력 스트림에서 문자열을 한 줄씩 추출합니다.

// (1) delimiter as parameter template<class CharType, class Traits, class Allocator> basic_istream<CharType, Traits>& getline(     basic_istream<CharType, Traits>& is,     basic_string<CharType, Traits, Allocator>& str, CharType delim);  template<class CharType, class Traits, class Allocator> basic_istream<CharType, Traits>& getline(     basic_istream<CharType, Traits>&& is,     basic_string<CharType, Traits, Allocator>& str, const CharType delim);  // (2) default delimiter used template<class CharType, class Traits, class Allocator> basic_istream<CharType, Traits>& getline(     basic_istream<CharType, Traits>& is,     basic_string<CharType, Traits, Allocator>& str);  template<class Allocator, class Traits, class Allocator> basic_istream<Allocator, Traits>& getline(     basic_istream<Allocator, Traits>&& is,     basic_string<Allocator, Traits, Allocator>& str); 

매개 변수

  • is
    문자열을 추출할 입력 스트림입니다.

  • str
    입력 스트림에서 문자를 읽어들일 문자열입니다.

  • delim
    줄 구분 기호입니다.

반환 값

입력 스트림 is입니다.

설명

(1)로 표시된 함수 시그니처 쌍은 delim이 발견될 때까지 is에서 문자를 추출하여 str에 저장합니다.

(2)로 표시된 함수 시그니처 쌍은 줄 바꿈 문자를 기본 줄 구분 기호로 사용하며 getline(is, str, is.widen('\n'))으로 동작합니다.

각 쌍의 두 번째 함수는 rvalue 참조를 지원하기 위한 첫 번째 함수와 유사한 버전입니다.

다음 중 하나가 발생하면 추출이 중지됩니다.

  • 파일의 끝에 도달하는 경우. 그러면 is의 내부 상태 플래그가 ios_base::eofbit로 설정됩니다.

  • 함수가 요소를 추출해서 비교한 결과 delim과 같은 것으로 확인되는 경우. 해당 요소는 되돌려지지도 않고 제어되는 시퀀스에 추가되지도 않습니다.

  • 함수가 str.max_size 요소를 추출한 후. 그러면 is의 내부 상태 플래그가 ios_base::failbit로 설정됩니다.

  • 위에 나와 있지 않은 기타 오류가 발생하는 경우. 그러면 is의 내부 상태 플래그가 ios_base::badbit로 설정됩니다.

내부 상태 플래그에 대한 자세한 내용은 ios_base::iostate를 참조하세요.

함수가 요소를 추출하지 않으면 is의 내부 상태 플래그는 ios_base::failbit로 설정됩니다. 어떤 경우든 getline은 is를 반환합니다.

예외가 throw되어도 is 및 str은 유효한 상태로 유지됩니다.

예제

다음 코드에서는 getline()을 두 가지 모드에서 보여 줍니다. 첫 번째 모드에서는 기본 구분 기호(줄 바꿈 문자)를 사용하고 두 번째 모드에서는 공백을 구분 기호로 사용합니다. 파일의 끝 문자(키보드에서 CTRL+Z를 누름)를 사용하여 while 루프 종료를 제어합니다. 그러면 cin의 내부 상태 플래그가 eofbit로 설정되며, basic_ios::clear()를 사용하여 이 상태를 해제해야 두 번째 while 루프가 정상적으로 작동합니다.

// compile with: /EHsc /W4
#include <string>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    string str;
    vector<string> v1;
    cout << "Enter a sentence, press ENTER between sentences. (Ctrl-Z to stop): " << endl;
    // Loop until end-of-file (Ctrl-Z) is input, store each sentence in a vector.
    // Default delimiter is the newline character.
    while (getline(cin, str)) {
        v1.push_back(str);
    }

    cout << "The following input was stored with newline delimiter:" << endl;
    for (const auto& p : v1) {
        cout << p << endl;
    }

    cin.clear();

    vector<string> v2;
    // Now try it with a whitespace delimiter
    while (getline(cin, str, ' ')) {
        v2.push_back(str);
    }

    cout << "The following input was stored with whitespace as delimiter:" << endl;
    for (const auto& p : v2) {
        cout << p << endl;
    }
}

출력

                            

요구 사항

헤더: <string>

네임스페이스: std

참고 항목

참조

basic_string 클래스

<string>