basic_string_getline
basic_string_getline (std::basic_string용 getline 함수)
이 페이지에서는 입력 스트림에서 문자를 읽어 문자열에 저장하는 getline 함수에 대해 알아봐요. 이 함수는 <string> 헤더에 정의되어 있으며, 줄 단위 입력 처리에 자주 사용돼요. 구분 문자를 지정하면 한 줄 전체뿐 아니라 줄의 일부도 읽을 수 있어요.
출처: cppreference
본문
함수 개요
getline은 입력 스트림에서 문자를 읽어 문자열에 저장하는 함수예요. 다음은 <string> 헤더에 정의된 네 가지 오버로드 형태예요.
| Defined in header |
||
|---|---|---|
| template < class CharT , class Traits , class Allocator > std :: basic_istream < CharT , Traits >& getline ( std :: basic_istream < CharT , Traits >& input , std :: basic_string < CharT , Traits , Allocator >& str , CharT delim ); | (1) | |
| template < class CharT , class Traits , class Allocator > std :: basic_istream < CharT , Traits >& getline ( std :: basic_istream < CharT , Traits >&& input , std :: basic_string < CharT , Traits , Allocator >& str , CharT delim ); | (2) | (since C++11) |
| template < class CharT , class Traits , class Allocator > std :: basic_istream < CharT , Traits >& getline ( std :: basic_istream < CharT , Traits >& input , std :: basic_string < CharT , Traits , Allocator >& str ); | (3) | |
| template < class CharT , class Traits , class Allocator > std :: basic_istream < CharT , Traits >& getline ( std :: basic_istream < CharT , Traits >&& input , std :: basic_string < CharT , Traits , Allocator >& str ); | (4) | (since C++11) |
getline은 입력 스트림에서 문자를 읽어 문자열에 넣어요.
Parameters
| input | - | 데이터를 가져올 스트림이에요 |
|---|---|---|
| str | - | 데이터를 저장할 문자열이에요 |
| delim | - | 구분 문자예요 |
Return value
input을 반환해요.
Notes
공백으로 구분된 입력을 처리할 때(예: int n; std::cin >> n;) 뒤따르는 공백(개행 문자 포함)은 입력 스트림에 남아 있어요. 그런 다음 줄 단위 입력으로 전환하면 getline으로 가져온 첫 줄은 그 공백이 돼요. 이런 동작이 바람직하지 않다면 다음과 같은 해결책을 사용할 수 있어요.
- 명시적으로
getline을 처음에 한 번 더 호출해요. std::cin >> std::ws로 연속된 공백을 제거해요.std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');로 입력 줄에 남은 모든 문자를 무시해요.
Example
다음 예제는 getline 함수를 사용해 사용자 입력을 읽고, 스트림을 줄 단위로 또는 delim 매개변수를 이용해 줄의 일부로 처리하는 방법을 보여줘요.
#include <iostream>
#include <sstream>
#include <string>
int main()
{
// greet the user
std::string name;
std::cout << "What is your name? ";
std::getline(std::cin, name);
std::cout << "Hello " << name << ", nice to meet you.\n";
// read file line by line
std::istringstream input;
input.str("1\n2\n3\n4\n5\n6\n7\n");
int sum = 0;
for (std::string line; std::getline(input, line);)
sum += std::stoi(line);
std::cout << "\nThe sum is " << sum << ".\n\n";
// use separator to read parts of the line
std::istringstream input2;
input2.str("a;b;c;d");
for (std::string line; std::getline(input2, line, ';');)
std::cout << line << '\n';
}
가능한 출력:
What is your name? John Q. Public
Hello John Q. Public, nice to meet you.
The sum is 28.
a
b
c
d
Defect reports
다음 동작 변경 결함 보고서는 이전에 발표된 C++ 표준에 소급 적용됐어요.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 91 | C++98 | getline did not behave as an unformatted input function | behaves as an unformatted input function |
See also
| getline | extracts characters until the given character is found (public member function of std::basic_istream<CharT,Traits> ) [edit] |
|---|