c语言中:
string 转 int,float
c语言中准确的说应该是char*类型转换成int和float等类型.
头文件: <stdlib.h> 对于c++可以用<cstdlib>
atoi 转int
atof 转float
strtod 转double
strtol转long
strtoul转unsigned long
反过来 int/float向char *的转换:
int,float to string
itoa from int
ltoa from long
ultoa from unsigned long
ftoa from float
sprintf函数方法实现;
sprintf( mystring, "%d", myint );
sprintf( mystring, "%f", myfloat );
c++中:
string to int, float
int,float to string
借道stringstream可以用于各种类型之间的数据转换.
即将int读入到stringstream对象中,然后stringstream对象读出到string对象中即可.反过来也可以.
#include <sstream>
stringstream sstrm;
string str("123");
int i;
sstrm<<str;
sstrm>>i;