将整型数据转换为string类型
在进行代码数据处理的时候,遇到数据类型转换的问题。因为string类型重载了+等操作符,处理数据起来比较方便,但是有些时候需要将其他类型的数据加到string类型中,这就出现了其他类型转换为string类型的问题。
要实现Int 等类型转换为string类型,首先要用系统函数ltoa 、itoa等转换为char* ,然后在转换为string中。
char c[20];
string buf,buf2;
buf = ltoa(23,c,10);
buf2 = itoa(32,c,10);
cout<<buf<<" "<<buf2<<endl;
char * __cdecl ltoa(__in long _Val, __out_z char * _DstBuf, __in int _Radix);
char * __cdecl itoa(__in int _Val, __out_z char * _DstBuf, __in int _Radix);
在网上还有很多其他方法,比如有stringstram的方式。
其中char* 、string、Cstring等区别可参考文章http://www.360doc.com/content/15/0803/19/21172899_489295148.shtml