Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

C++에서 int를 문자열로 변환하는 방법은 무엇입니까?


C에서 itoa 함수를 사용하여 int를 문자열로 변환할 수 있습니다.

#include<iostream>
int main() {
   int a = 10;
   char *intStr = itoa(a);
   string str = string(intStr);
   cout << str;
}

출력

이것은 출력을 줄 것입니다 -

10

이는 정수를 문자열로 변환합니다. C++11에서는 같은 용도로 사용할 수 있는 새로운 메소드인 to_string이 추가되었습니다. 다음과 같이 사용할 수 있습니다 -

예시

#include<iostream>
using namespace std;
int main() {
   int a = 10;
   string str = to_string(a);
   cout << str;
}

출력

이것은 출력을 제공합니다 -

10