int 在linux中怎么转化为string ,itoa不可以,还有别的办法吗?

VCRWX 2009-03-11 09:30:57
int 在linux中怎么转化为string ,itoa不可以,还有别的办法吗?
...全文
3703 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiangweixing 2009-03-14
  • 打赏
  • 举报
回复
sprintf基本上在所有能使用C的系统上都可以使用
yangch_nhcmo 2009-03-11
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int i = 123456;
ostringstream ostr;
ostr << i ;

string str(ostr.str());
cout << str << endl;

return 0;
}


yangch_nhcmo 2009-03-11
  • 打赏
  • 举报
回复

#include <stdio.h>

int main()
{
int i = 123456;
char s[10];
sprintf(s,"%d",i);

printf("%s\n",s);

return 0;
}

夹心饼干 2009-03-11
  • 打赏
  • 举报
回复
int m = 123214;
char buf[20];
memset(buf,0x00,sizeof(buf));
sprintf(buf,"%d",m);
string str = buf;
  • 打赏
  • 举报
回复
请问linux下不可以用string吗?
先itoa转为字符串,然后付给string啊。
lightnut 2009-03-11
  • 打赏
  • 举报
回复
用ostringstream

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
int a = 10;
ostringstream ostr;
ostr << 10;
string astr = ostr.str();
cout << astr <<endl;

return 0;
}
yutaooo 2009-03-11
  • 打赏
  • 举报
回复

C 里用 sprintf
C++ 里用 boost::lexical_cast
沙漠里的海豚 2009-03-11
  • 打赏
  • 举报
回复


可以用sprintf实现的,呵呵
int i = 123;
char string[10];
sprintf(string, "%d", i);

就可以啦

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧