C++里面怎么把int转换成char类型?

比如我现在有一个int类型数123我要把它转换成字符数组'1''2''3'该怎么转换?不要那种stdio.h的办法,我这个程序用的iostream写的

用库函数itoa很方便
#include <iostream>
using namespace std;
int main()
{
int a = 123;
char str[100];
itoa(a, str, 10);
cout << str << endl;
return 0;
}
如果觉得itoa没有c++味道,那可以用流对象转换:
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
int a = 123;
ostringstream s;
s << a;
cout <<s.str() << endl;
return 0;
}
温馨提示:答案为网友推荐,仅供参考
相似回答