티스토리 뷰
함수 | 설명 |
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
}
'C++' 카테고리의 다른 글
[C++] 정적 라이브러리 생성하고 활용하기 (0) | 2023.09.10 |
---|---|
[C++] 프로젝트 초기 셋업은 어떻게?? (0) | 2023.09.09 |
[C++] 엔디언 변환 클래스 (2,4,8 byte 전용) (0) | 2023.07.24 |
[STL] 메모리 관리 하느라 정신 나갈 것 같을 때, 스마트 포인터(Smart Pointer) (0) | 2023.07.02 |
[STL] STL 에 댄한 배경 지식 (0) | 2023.07.01 |