string

왕준영·2022년 11월 25일
1

C++ STL

목록 보기
3/6

출처 : https://learn.microsoft.com/ko-kr/cpp/standard-library/string-functions?view=msvc-170

Haeder

#include <string>

Functions

getline

  • in_stream 에서 delimiter를 기준으로 str에 한줄씩 추출함

  • 공백 문자 포함 입력에서 많이 사용

  • 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);
  • 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);
int main(){
    string str;

    getline(cin,str);
    cout<<str<<'\n';

    while(getline(cin,str,',')){
        cout<<str<<'\n';
    }
}

to_string

  • 다른 data type의 variable 을 string type으로 casting
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);

-Example

int main(){
	int v=INT32_MAX;
    unsigned int uv=INT32_MAX;

    long lv=INT32_MAX;
    unsigned long ulv=INT32_MAX;

    long long llv=INT64_MAX;
    unsigned long long ullv=INT64_MAX;

    float fv=0.000001;
    double dv=0.000001;
    long double ldv = 0.000001;
}

stoi

  • str 을 base 를 기수로 int type 으로 casting
int stoi(
    const string& str,
    size_t* idx = 0,
    int base = 10);

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

stol

  • str 을 base 를 기수로 long type 으로 casting
long stol(
    const string& str,
    size_t* idx = 0,
    int base = 10);

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

stoul

  • str 을 base 를 기수로 unsigned long type 으로 casting
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);

stoll

  • str 을 base 를 기수로 long long type 으로 casting
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);

stoull

  • str 을 base 를 기수로 unsigned long long type 으로 casting
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);

stof

  • str을 float type으로 casting
float stof(
    const string& str,
    size_t* idx = 0);

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

stod

  • str을 double type으로 casting
double stod(
    const string& str,
    size_t* idx = 0);

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

stold

  • str을 long double type으로 casting
double stold(
    const string& str,
    size_t* idx = 0);

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

sto_type Examples

int main(){
    string S_BASE_2="110";
    string S_BASE_10="6";

    if(stoi(S_BASE_2,0,2)==stoi(S_BASE_10))
        cout<<"true"<<'\n';

    cout << stol("123456789") <<'\n';
    cout << stoul("12345689") <<'\n';
    cout << stoll("123456789123456789") <<'\n';
    cout << stoull("123456789123456789") <<'\n';
    cout << stof("3.14") <<'\n';
    cout << stod("3.141592653589") <<'\n';
    cout << stold("3.141592653589") <<'\n';
}
profile
원리,구현,근본

0개의 댓글