프로그래밍 언어/C++

[C++] stoi, stof, stol, stod 함수에 대해서 (string to int), 문자열 > 특정 타입

ShovelingLife 2023. 7. 20. 10:50

1. C++에서 string 타입의 문자열을 숫자로 바꾸는 함수들의 이름.

 C++11 부터 아래 함수들을 사용할 수 있다.

stoi = string to int

stof = string to float

stol = string to long

stod = string to double

stoul = string to unsigned int

stoll = string to long long

stoull = string to unsigned long long

stold = string to long double

2. C++ stoi, stof, stol, stod 함수 원형과 매개변수가 뜻하는 것

▼ 함수 원형

1) 정수형

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

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

 

2) 실수형

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

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

 

 매개변수가 뜻하는 것들

함수원형1 : int stoi(const string& str, size_t* idx =0, int base = 10);

함수원형2 : float stof(const string& str, size_t* idx = 0);

 

첫번째 인자 : const string& str: 변경할 문자열이 들어가게 된다. 문자열 복사의 비용이 들어가지 않도록 & (reference)를 이용해서 넘기게 된다. 또한 함수 내부에서 변경이 일어나지 않음을 보장하기 위해서 const 상수 취급해서 넘기게 된다.


두번째 인자 : size_t* idx = 0: 두번째는 포인터 타입이고 맨 첫번째 부터 숫자가 아닌 부분까지 해서 그부분의 포인터를 걸러준다. 세번째 인자까지 사용하는데, 두번째 인자는 사용하지 않겠다 하면 nullptr을 넣으면 된다.

 

세번째 인자 : int base = 10 (정수형에만 존재): base 는 진수를 뜻하는 것이다. default가 10으로 되어있다는건 10진수가 기본이라는 뜻이고 string 안에 있는 숫자의 표현이 어떤것이다 라고 base 를 통해서 알려주는 것 이다.: binary (2진수)이라면 2를 넣고: octal (8진수)이라면 8을 넣고: decimal (10진수)는 기본이니까 넣지 않아도 된다. hexadecimal (16진수)이라면 16을 넣으면 된다.

예제

//[C++] stirng to integer example1
//BlockDMask
 
#include<iostream>
#include<string> //need this
using namespace std;
int main(void)
{
    string str_i = "22";
    string str_li = "2144967290";
    string str_f = "3.4";
    string str_d = "2.11";
 
    //int i = str_i;    //error
    int i       = stoi(str_i);
    long int li = stol(str_li);
    float f     = stof(str_f);
    double d    = stod(str_d);
 
    //C++ cout
    cout << "stoi : " << i    << endl;
    cout << "stol : " << li  << endl;
    cout << "stof : " << f    << endl;
    cout << "stod : " << d    << endl;
 
    cout << endl;
    //C, C++ printf
    printf("stoi : %d\n", i);
    printf("stol : %ld\n", li);
    printf("stof : %f\n", f);
    printf("stod : %lf\n", d);
 
    cout << endl;
    system("pause");
    return 0;
}

C++ stoi, stol, stod, stof 함수 예제2 (매개변수들을 이용한 응용 사용법)

//[C++] stirng to integer example2
//BlockDMask
 
#include<iostream>
#include<string> //need this
using namespace std;
int main(void)
{
    //mixed string
    string str1 = "33BlockDMask";
 
    //test1
    int num1 = stoi(str1);
    cout << "test1. stoi(str1);" << endl;
    cout << " - str1 : " << str1 << endl;
    cout << " - num1 : " << num1 << endl;
    cout << endl;
 
    //test2
    size_t sz;
    int num2 = stoi(str1, &sz);
    cout << "test2. stoi(str1, &sz);" << endl;
    cout << " - str1 : " << str1 << endl;
    cout << " - num2 : " << num2 << endl;
    cout << " - sz : " << sz << endl;
    cout << " - str1[sz] : " << str1[sz] << endl;
    cout << " - str1.substr(sz) : " << str1.substr(sz) << endl;
 
    cout << endl;
    //test3
    string str2 = "";
    cout << "test3. stoi(str2, nullptr, base);" << endl;
    
    //string binary -> int
    str2 = "1110";
    cout << " - stoi(1110, nullptr, 2) : " << stoi(str2, nullptr, 2) << endl;
    
    //string oct -> int
    str2 = "014";
    cout << " - stoi(014 , nullptr, 8) : " << stoi(str2, nullptr, 8) << endl;
 
    //string hex -> int
    str2 = "0x14";
    cout << " - stoi(0x14, nullptr, 16) : " << stoi(str2, nullptr, 16) << endl;
    
    cout << endl;
    system("pause");
    return 0;
}

출처 : [C++] stoi, stof, stol, stod 함수에 대해서 (string to int) (tistory.com)