코딩 학습/C와 C++

C++ 기초 - 문자열 -> 숫자 변환

이개 2026. 3. 24. 11:47

문자열 -> 숫자 변환하는 함수

 

1. stoi, stof, stod, stol

std::string str = "42";

int n    = std::stoi(str);   // string to int
float f  = std::stof(str);   // string to float
double d = std::stod(str);   // string to double
long l   = std::stol(str);   // string to long

 

2. stringstream

#include <sstream>

std::string str = "42";
std::stringstream ss(str);
int n;
ss >> n; // 자바의 Integer.parseInt() 느낌