티스토리 뷰

C++

[C++] string 변환 함수 정리

권벡터 2023. 7. 19. 23:30
함수 설명
stoi string -> int
stol string -> long
stoll string -> long long
atoi byte string(= const char*) -> int
atol byte string(= const char*) -> long
atoll byte string(= const char*) -> long
stod byte string(= const char*) -> double
stof byte string(= const char*) -> float
stold byte string(= const char*) -> long double
stoul string -> unsigned long
stoull string -> uinsigned long long

 

stoi 예시

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string testStr1 = "12";
    string testStr2 = "3.543";
    string testStr3 = "84832 여기 글씨 있다.";


    int convertedint1 = stoi(testStr1);
    int convertedint2 = stoi(testStr2);
    int convertedint3 = stoi(testStr3);

    cout << "stoi(\"" << testStr1 << "\") is " << convertedint1 << '\n';
    cout << "stoi(\"" << testStr2 << "\") is " << convertedint2 << '\n';
    cout << "stoi(\"" << testStr3 << "\") is " << convertedint3 << '\n';
    
//stoi("12") is 12
//stoi("3.543") is 3
//stoi("84832 여기 글씨 있다.") is 84832
}

atoi 예시

#include <iostream>
#include <cstdlib>

using namespace std;
int main()
{
    const char* str1 = "12";
    const char* str2 = "4.253";
    const char* str3 = "54321 여기 글씨 있대두";
    const char* str4 = "메롱 3";

    int num1 = atoi(str1);
    int num2 = atoi(str2);
    int num3 = atoi(str3);
    int num4 = atoi(str4);

    cout << "atoi(\"" << str1 << "\") is " << num1 << '\n';
    cout << "atoi(\"" << str2 << "\") is " << num2 << '\n';
    cout << "atoi(\"" << str3 << "\") is " << num3 << '\n';
    cout << "atoi(\"" << str4 << "\") is " << num4 << '\n';
    
//atoi("12") is 12
//atoi("4.253") is 4
//atoi("54321 여기 글씨 있대두") is 54321
//atoi("메롱 3") is 0
}

문장에 숫자가 아닌 문자가 먼저 나오면 정수로 변환시켜주지 않는다.

 

stod 예시

#include <iostream>
#include<string>
using namespace std;

int main()
{
    string str1 = "13";
    string str2 = "5.432";
    string str3 = "2323.1111111 with char";

    double num1 = stod(str1);
    double num2 = stod(str2);
    double num3 = stod(str3);
    
    cout << "stod(\"" << str1 << "\") is " << num1 << '\n';
    cout << "stod(\"" << str2 << "\") is " << num2 << '\n';
    cout << "stod(\"" << str3 << "\") is " << num3 << '\n';
//stod("13") is 13
//stod("5.432") is 5.432
//stod("2323.1111111 with char") is 2323.11
}

문자와 섞은 string은 double로 변환해주지만 소수점 2자리 까지 변환해준다.

 

stringstream 사용하여 변환하는 방법

#include <iostream> 
#include <sstream> 
using namespace std;

int main()
{
    
    string str = "2508";
    string doublestr = "3.142";
    string strWithChar = "3.2321 아무말";

    //저장 할 변수
    int num = 0;
    double dNum1 = 0.0;
    double dNum2=0.0;

    //변환 
    stringstream sstream(str);
    sstream >> num;
    stringstream dstream1(doublestr);
    dstream1 >> dNum1;
    stringstream dstream2(strWithChar);
    dstream2 >> dNum2;

    // 저장된 변수를 출력
    cout << "Value of num : " << num << endl;
    cout << "Value of dNum1 : " << dNum1 << endl;
    cout << "Value of dNum2 : " << dNum2;

    return 0;
//Value of num : 2508
//Value of dNum1 : 3.142
//Value of dNum2 : 3.2321
}

 

to_string() 사용하여 변환하는 법

#include<iostream> 
#include<string>
using namespace std;
int main()
{
    int int_val = 20;
    float float_val = 30.50;
    double double_val = 7.123;

    string str_int = to_string(int_val);
    string str_float = to_string(float_val);
    string str_double = to_string(double_val);

    cout << "string -> int : ";
    cout << str_int << endl;
    cout << "string -> float : ";
    cout << str_float << endl;
    cout << "string -> double : ";
    cout << str_double << endl;
    return 0;
//string -> int : 20
//string -> float : 30.500000
//string -> double : 7.123000
    
}

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함