어흥

[C++] String <-> Int 변환 본문

C++

[C++] String <-> Int 변환

라이언납시오 2020. 6. 3. 23:07
728x90
반응형

1. string -> int로 변환 (stdlib.h 필요)

- atoi(XX.c_str());

- stoi(str);

 

2. int -> string으로 변환

:to_string(YY);

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

int main() {
	//출력: 123 369
	string str = "123";
	int a = atoi(str.c_str());
	int b = 246;
	cout << a << " " << a + b << '\n';

	//출력: abc369
	string s1 = "abc";
	string s2 = to_string(a + b);
	cout << s1 + s2;
	system("pause");
	return 0;
}

 

 

728x90
반응형
Comments