Language/C++

[C++] string to / from string

리네엔 2024. 10. 24. 10:07

Converting a string to another type

string to int std::stoi()

std::string str = "123";
int value = std::stoi(str);  // value = 123

string to long std::stol()

std::string str = "123456";
long value = std::stol(str);  // value = 123456

string to longlong std::stoll()

std::string str = "1234567890";
long long value = std::stoll(str);  // value = 1234567890

string to float std::stof()

std::string str = "3.14";
float value = std::stof(str);  // value = 3.14f

string to double std::stod()

std::string str = "3.14159";
double value = std::stod(str);  // value = 3.14159

stirng to long double std::stold()

std::string str = "3.1415926535";
long double value = std::stold(str);  // value = 3.1415926535L

string to unsigned long std::stoul()

string to unsigned longlong std::stoull()

Converting another type to a string

std::to_string

int num = 42;
std::string str = std::to_string(num);  // str = "42"

'Language > C++' 카테고리의 다른 글

[C++] 형변환  (0) 2024.10.24
[C++] transform  (1) 2024.10.24