C++字符串与数字类型互转

通过string::to_string()​函数可以从整数/浮点数转换为字符串,通过atoi()或atof()等方法可以将字符串转换为整数或浮点数。

int dec = -100;
string dstr = to_string(dec);
int dec1 = atoi(dstr.c_str());
​
float fnum = 3.14f;
string fstr = to_string(fnum);
float fnum1 = atof(fstr.c_str());
c++
121 views
Comments
登录后评论
Sign In