-
C++ stoi 함수 | C++ string to integer유용한 정보 2021. 12. 7. 22:03반응형
코딩테스트 문제풀이를 하다보면 string형 변수를 integer형으로 변환해야하는 경우가 빈번하게 발생합니다. 이러한 경우 사용할 수 있는 함수가 stoi 함수입니다.
1. string 헤더를 참조하여야 합니다.
2. stoi( string s ) 의 형태를 취해야 합니다.
#include <string> #include <vector> #include <map> #include <iostream> using namespace std; int main() { string s ="123456789"; cout << "string s value : "<< s<<'\n'; int sInt = stoi(s); cout << "converted s value(INT) : "<< sInt<<'\n'; return 0; }
stoi는 선행 공백 문자,+/-기호, 16 진수 접두사 (0x 또는0X), 여러 개의 0을 처리하고 정수를 올바르게 반환 할 수 있습니다. 숫자 앞의 다른 문자는 처리 할 수 없으며, 하나를 찾으면std::invalid_argument 예외가 발생합니다.
반응형'유용한 정보' 카테고리의 다른 글
우선순위 큐 HEAP ||C++ STL priority queue (0) 2021.12.24 순열과 조합의 차이 (0) 2021.12.16 C++ map STL 사용법 (0) 2021.12.05 string split 하는법 C++ | sstream 사용법 C++ | find함수 substr함수 (0) 2021.12.02 C++ sort함수 cmp 함수 (0) 2021.10.08