<string> 함수

getline
stod
stof
stoi
stol
stold
stoll
stoul
stoull
swap
to_string
to_wstring

getline

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

// (1) delimiter as parameter
template <class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(
    basic_istream<CharType, Traits>& in_stream,
    basic_string<CharType, Traits, Allocator>& str,
    CharType delimiter);

template <class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(
    basic_istream<CharType, Traits>&& in_stream,
    basic_string<CharType, Traits, Allocator>& str,
    const CharType delimiter);

// (2) default delimiter used
template <class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(
    basic_istream<CharType, Traits>& in_stream,
    basic_string<CharType, Traits, Allocator>& str);

template <class Allocator, class Traits, class Allocator>
basic_istream<Allocator, Traits>& getline(
    basic_istream<Allocator, Traits>&& in_stream,
    basic_string<Allocator, Traits, Allocator>& str);

매개 변수

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

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

delimiter
줄 구분 기호입니다.

Return Value

입력 스트림 in_stream입니다.

설명

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

표시된 (2) 함수 서명 쌍은 줄 바꿈을 기본 줄 구분 기호로 사용하고 다음과 같이 getline(in_stream, str, in_stream. widen('\n'))동작합니다.

각 쌍의 두 번째 함수는 참조를 지원하는 rvalue 첫 번째 함수와 유사합니다.

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

  • 파일의 끝에서 내부 상태 플래그 in_stream 가 .로 설정 ios_base::eofbit됩니다.

  • 함수가 같 delimiter음과 비교하는 요소를 추출한 후 . 요소는 제어되는 시퀀스에 다시 배치되거나 추가되지 않습니다.

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

  • 이전에 나열된 오류 이외의 다른 오류입니다. 내부 상태 플래그 in_stream 는 .로 설정됩니다 ios_base::badbit.

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

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

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

예시

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

// 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;
    }
}

stod

문자 시퀀스를 double으로 변환합니다.

double stod(
    const string& str,
    size_t* idx = 0);

double stod(
    const wstring& str,
    size_t* idx = 0
;

매개 변수

str
변환할 문자 시퀀스입니다.

idx
변환되지 않은 첫 번째 문자의 인덱스 값입니다.

Return Value

double 값입니다.

설명

함수는 요소 str 시퀀스를 호출strtod( str.c_str(), _Eptr)하는 것처럼 형식 double 값으로 변환합니다. 여기서 함수의 내부 개체는 다음과 _Eptr 같습니다. 이 경우 str.c_str() == *_Eptr형식 invalid_argument의 개체를 throw합니다. 이러한 호출에서 errno를 설정하는 경우에는 out_of_range 형식의 개체가 throw됩니다. 그렇지 않으면 null 포인터가 아닌 경우 idx 함수는 값을 저장 *_Eptr - str.c_str()*idx 하고 반환합니다.

stof

문자 시퀀스를 float로 변환합니다.

float stof(
    const string& str,
    size_t* idx = 0);

float stof(
    const wstring& str,
    size_t* idx = 0);

매개 변수

str
변환할 문자 시퀀스입니다.

idx
변환되지 않은 첫 번째 문자의 인덱스 값입니다.

Return Value

float 값입니다.

설명

함수는 요소 str 시퀀스를 호출strtof( str.c_str(), _Eptr)하는 것처럼 형식 float 값으로 변환합니다. 여기서 함수의 내부 개체는 다음과 _Eptr 같습니다. 이 경우 str.c_str() == *_Eptr형식 invalid_argument의 개체를 throw합니다. 이러한 호출에서 errno를 설정하는 경우에는 out_of_range 형식의 개체가 throw됩니다. 그렇지 않으면 null 포인터가 아닌 경우 idx 함수는 값을 저장 *_Eptr - str.c_str()*idx 하고 반환합니다.

stoi

문자 시퀀스를 정수로 변환합니다.

int stoi(
    const string& str,
    size_t* idx = 0,
    int base = 10);

int stoi(
    const wstring& str,
    size_t* idx = 0,
    int base = 10);

Return Value

정수 값입니다.

매개 변수

str
변환할 문자 시퀀스입니다.

idx
변환되지 않은 첫 번째 문자의 인덱스 값입니다.

base
사용할 기수입니다.

설명

함수 stoi 는 str의 문자 시퀀스를 형식 int 값으로 변환하고 값을 반환합니다. 예를 들어 문자 시퀀스 "10" 전달 시 stoi에서 반환하는 값은 정수 10입니다.

stoi는 함수 내부 개체인 경우 또는 유사한 방식으로 wcstol(Str.c_str(), _Eptr, idx)호출되는 와이드 문자의 경우 _Eptrstrtol( str.c_str(), _Eptr, idx)싱글 바이트 문자에 대한 함수 wcstol 와 유사하게 strtol 동작합니다. 자세한 내용은 , , wcstol_strtol_l_wcstol_l를 참조하세요.strtol

stoi 이면 str.c_str() == *_Eptr형식invalid_argument의 개체를 throw합니다. 이러한 호출이 설정errno되거나 반환된 값을 형식의 개체로 나타낼 수 없는 경우 형식intout_of_range의 개체를 throw합니다. 그렇지 않으면 idx가 null 포인터가 아닌 경우 함수는 에 저장됩니다 *_Eptr - str.c_str()*idx.

stol

문자 시퀀스를 long으로 변환합니다.

long stol(
    const string& str,
    size_t* idx = 0,
    int base = 10);

long stol(
    const wstring& str,
    size_t* idx = 0,
    int base = 10);

매개 변수

str
변환할 문자 시퀀스입니다.

idx
변환되지 않은 첫 번째 문자의 인덱스 값입니다.

base
사용할 기수입니다.

Return Value

long 정수 값입니다.

설명

함수는 함수의 내부 개체인 경우를 호출 strtol( str.c_str(), _Eptr, idx)_Eptr 하는 것처럼 str의 요소 시퀀스를 형식 long 값으로 변환합니다. 이 경우 str.c_str() == *_Eptr형식 invalid_argument의 개체를 throw합니다. 이러한 호출에서 errno를 설정하는 경우에는 out_of_range 형식의 개체가 throw됩니다. 그렇지 않으면 null 포인터가 아닌 경우 idx 함수는 값을 저장 *_Eptr - str.c_str()*idx 하고 반환합니다.

stold

문자 시퀀스를 long double으로 변환합니다.

double stold(
    const string& str,
    size_t* idx = 0);

double stold(
    const wstring& str,
    size_t* idx = 0);

매개 변수

str
변환할 문자 시퀀스입니다.

idx
변환되지 않은 첫 번째 문자의 인덱스 값입니다.

Return Value

long double 값입니다.

설명

함수는 함수의 내부 개체인 경우를 호출 strtold( str.c_str(), _Eptr)_Eptr 하는 것처럼 str의 요소 시퀀스를 형식 long double 값으로 변환합니다. 이 경우 str.c_str() == *_Eptr형식 invalid_argument의 개체를 throw합니다. 이러한 호출에서 errno를 설정하는 경우에는 out_of_range 형식의 개체가 throw됩니다. 그렇지 않으면 null 포인터가 아닌 경우 idx 함수는 값을 저장 *_Eptr - str.c_str()*idx 하고 반환합니다.

stoll

문자 시퀀스를 long long으로 변환합니다.

long long stoll(
    const string& str,
    size_t* idx = 0,
    int base = 10);

long long stoll(
    const wstring& str,
    size_t* idx = 0,
    int base = 10);

매개 변수

str
변환할 문자 시퀀스입니다.

idx
변환되지 않은 첫 번째 문자의 인덱스 값입니다.

base
사용할 기수입니다.

Return Value

long long 값입니다.

설명

함수는 함수의 내부 개체인 경우를 호출 strtoll( str.c_str(), _Eptr, idx)_Eptr 하는 것처럼 str의 요소 시퀀스를 형식 long long 값으로 변환합니다. 이 경우 str.c_str() == *_Eptr형식 invalid_argument의 개체를 throw합니다. 이러한 호출에서 errno를 설정하는 경우에는 out_of_range 형식의 개체가 throw됩니다. 그렇지 않으면 idx 가 null 포인터가 아니면 함수가 값을 저장 *_Eptr - str.c_str()*idx 하고 반환합니다.

stoul

문자 시퀀스를 부호 없는 long으로 변환합니다.

unsigned long stoul(
    const string& str,
    size_t* idx = 0,
    int base = 10);

unsigned long stoul(
    const wstring& str,
    size_t* idx = 0,
    int base = 10);

매개 변수

str
변환할 문자 시퀀스입니다.

idx
변환되지 않은 첫 번째 문자의 인덱스 값입니다.

base
사용할 기수입니다.

Return Value

부호 없는 long 정수 값입니다.

설명

함수는 함수의 내부 개체인 경우를 호출 strtoul( str.c_str(), _Eptr, idx)_Eptr 하는 것처럼 str의 요소 시퀀스를 형식 unsigned long 값으로 변환합니다. 이 경우 str.c_str() == *_Eptr형식 invalid_argument의 개체를 throw합니다. 이러한 호출에서 errno를 설정하는 경우에는 out_of_range 형식의 개체가 throw됩니다. 그렇지 않으면 idx 가 null 포인터가 아니면 함수가 값을 저장 *_Eptr - str.c_str()*idx 하고 반환합니다.

stoull

문자 시퀀스를 unsigned long long으로 변환합니다.

unsigned long long stoull(
    const string& str,
    size_t* idx = 0,
    int base = 10);

unsigned long long stoull(
    const wstring& str,
    size_t* idx = 0,
    int base = 10);

매개 변수

str
변환할 문자 시퀀스입니다.

idx
변환되지 않은 첫 번째 문자의 인덱스 값입니다.

base
사용할 기수입니다.

Return Value

unsigned long long 값입니다.

설명

함수는 함수의 내부 개체인 경우를 호출 strtoull( str.c_str(), _Eptr, idx)_Eptr 하는 것처럼 str의 요소 시퀀스를 형식 unsigned long long 값으로 변환합니다. 이 경우 str.c_str() == *_Eptr형식 invalid_argument의 개체를 throw합니다. 이러한 호출에서 errno를 설정하는 경우에는 out_of_range 형식의 개체가 throw됩니다. 그렇지 않으면 null 포인터가 아닌 경우 idx 함수는 값을 저장 *_Eptr - str.c_str()*idx 하고 반환합니다.

swap

두 문자열의 문자 배열을 교환합니다.

template <class Traits, class Allocator>
void swap(basic_string<CharType, Traits, Allocator>& left, basic_string<CharType, Traits, Allocator>& right);

매개 변수

left
요소가 다른 문자열의 요소와 교환될 문자열입니다.

right
요소가 첫 번째 문자열과 교환되는 다른 문자열입니다.

설명

템플릿 함수는 문자열에 대해 특수 멤버 함수 left.swap(right)를 실행하여 일정한 복잡성을 보장합니다.

예시

// string_swap.cpp
// compile with: /EHsc
#include <string>
#include <iostream>

int main( )
{
   using namespace std;
   // Declaring an object of type basic_string<char>
   string s1 ( "Tweedledee" );
   string s2 ( "Tweedledum" );
   cout << "Before swapping string s1 and s2:" << endl;
   cout << "The basic_string s1 = " << s1 << "." << endl;
   cout << "The basic_string s2 = " << s2 << "." << endl;

   swap ( s1 , s2 );
   cout << "\nAfter swapping string s1 and s2:" << endl;
   cout << "The basic_string s1 = " << s1 << "." << endl;
   cout << "The basic_string s2 = " << s2 << "." << endl;
}
Before swapping string s1 and s2:
The basic_string s1 = Tweedledee.
The basic_string s2 = Tweedledum.

After swapping string s1 and s2:
The basic_string s1 = Tweedledum.
The basic_string s2 = Tweedledee.

to_string

값을 string로 변환합니다.

string to_string(int value);
string to_string(unsigned int value);
string to_string(long value);
string to_string(unsigned long value);
string to_string(long long value);
string to_string(unsigned long long value);
string to_string(float value);
string to_string(double value);
string to_string(long double value);

매개 변수

value
변환할 값입니다.

Return Value

값을 나타내는 string입니다.

설명

함수는 값을 배열 개체 Buf 내부에 저장된 요소 시퀀스로 변환하고, 여기서는 다음과 Fmt 같이 함수sprintf(Buf, Fmt, value)로 변환합니다.

  • "%d"형식이면 valueint

  • "%u"형식이면 valueunsigned int

  • "%ld"형식이면 valuelong

  • "%lu"형식이면 valueunsigned long

  • "%lld"형식이면 valuelong long

  • "%llu"형식이면 valueunsigned long long

  • "%f" 형식이면 value >이고 float , double

  • "%Lf"형식이면 valuelong double

함수에서 string(Buf)을 반환합니다.

to_wstring

값을 와이드 문자열로 변환합니다.

wstring to_wstring(int value);
wstring to_wstring(unsigned int value);
wstring to_wstring(long value);
wstring to_wstring(unsigned long value);
wstring to_wstring(long long value);
wstring to_wstring(unsigned long long value);
wstring to_wstring(float value);
wstring to_wstring(double value);
wstring to_wstring(long double value);

매개 변수

value
변환할 값입니다.

Return Value

값을 나타내는 와이드 문자열입니다.

설명

이 함수는 value가 있는 위치에서 Buf를 호출하는 것과 같이 swprintf(Buf, Len, Fmt, value)을 함수 내부의 배열 개체 Fmt에 저장된 요소 시퀀스로 변환합니다.

  • L"%d"형식이면 valueint

  • L"%u"형식이면 valueunsigned int

  • L"%ld"형식이면 valuelong

  • L"%lu"형식이면 valueunsigned long

  • L"%lld"형식이면 valuelong long

  • L"%llu"형식이면 valueunsigned long long

  • L"%f" 형식이면 value >이고 float , double

  • L"%Lf"형식이면 valuelong double

함수에서 wstring(Buf)을 반환합니다.

참고 항목

<string>