프로그래밍/C,C++

C++ 11, stoi, stof, stol, sod 문자열을 숫자로 바꾸는 함수

gameObject 2023. 7. 18. 08:32
728x90

C++11 부터는 string 클래스 숫자로 변경해주는 함수가 있다.

 

#include<string> 클래스에 정의되어있다.

 

stoi = string to int

stof = string to float

stol = string to long

stod = string to double

 

 

int num1 = stoi(str1)

str 입력하는값 -> 출력값

"123"  -> 123

"-123"  -> -123

"1.23"  -> 1

"123한글" -> 123

 

코딩테스트에서 문자형태로 주어진 특정 숫자를 찾아서 풀어야 하는 문제를 풀때 유용하다.

728x90