请问:C++如何将int转换成string类型

ybw_zjut 2006-07-24 09:40:05
请问:C++如何将int转换成string类型
强烈感谢!!!!!!
...全文
205 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
paobo 2006-07-24
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;


int int2str(int num, char * str, int len)
{
int sign, count;
char buf[12] = {0};

sign = num<0 ? -1:1; //标志位
num *= sign;

for(count=0; num; num/=10, count++)
buf[count] = num%10 + 48;

if(sign<0) buf[count++] = '-';
if (len < count) return -1;

while(count)
*(str++) = buf[count---1];

return 0;
}
main()
{
int nT = 156;
char buff[10] ={0};
int2str(nT,buff,3);

cout<<buff[0]<<endl;
cout<<buff[1]<<endl;
cout<<buff[2]<<endl;

}
du51 2006-07-24
  • 打赏
  • 举报
回复
boost用的还是stringstream所以,必须可以>> <<

C++里标准的就是stringstream
sinall 2006-07-24
  • 打赏
  • 举报
回复
1)itoa
2)ostringstream
3)boost::lexical_cast
睡在床板下_ 2006-07-24
  • 打赏
  • 举报
回复
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
stringstream sstr;
//--------int转string-----------
int a=100;
string str;
sstr<<a;
sstr>>str;
cout<<str<<endl;
//--------string转char[]--------
sstr.clear();//如果你想通过使用同一stringstream对象实现多种类型的转换,请注意在每一次转换之后都必须调用clear()成员函数。
string name = "colinguan";
char cname[200];
sstr<<name;
sstr>>cname;
cout<<cname;
system("pause");
}
kingsun555 2006-07-24
  • 打赏
  • 举报
回复
sprintf( buff , "%d" , i ) ;

string str ( buff ) ;

这样也可以吧
Veiz 2006-07-24
  • 打赏
  • 举报
回复
自己写一个函数 string ConvertIntToString(const int& integer);
zg1981 2006-07-24
  • 打赏
  • 举报
回复
itoa

64,648

社区成员

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

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