C++
[c++] string을 int로 변환하기
슥지니
2020. 3. 25. 09:52
반응형
[c++]
int atoi (const char * str) - ascii to integer
1
2
3
4
5
6
7
8
9
10
11
|
#include <iostream>
#include <string>
using namespace std;
int main(void) {
string str = "1234";
//atoi 파라미터는 char* 이므로 stirng을 char* 변환해서 인자로 넘겨야함
int num = atoi(str.c_str()); // ascii to integer
cout << num;
return 0;
}
|
cs |
[c++ 11 ~]
int stoi (const string& str, size_t* idx = 0, int base = 10);
첫 번째 인자: 정수로 표현할 문자열
두 번째 인자: 정수 다음에 나온 문자의 포인터를 가리킨다.
ex("1234abcd"이면 'a'를 위치를 가리킨다 -> idx = 4)
세 번째 인자: 진수
1
2
3
4
5
6
7
8
9
10
|
#include <iostream>
#include <string>
using namespace std;
int main(void) {
string str = "1234";
int num = stoi(str); // string to integer
cout << num;
return 0;
}
|
cs |
반응형