头文件是
#include<string>
一、string转其他类型
1、string转int
// string转int
string str = "123";
int num = atoi(str.c_str());
cout << num << endl;
// 123
2、string转long int
string str = "1234567890";
long int num = atol(str.c_str());
cout << num << endl;
// 1234567890
3、string转float
string str = "12.326374837251427";
double num = atof(str.c_str());
cout << num << endl;
// 12.3264
二、其他类型转string
如int、long int、float等类型转换为string时,都可用to_string()函数来转换,这里仅以int、float示例。
1、int转string
int num = 123;
string str = to_string(num);
cout << str << endl;
// 123
2、float转string
float num = 12.345432;
string str = to_string(num);
cout << str << endl;
// 12.345432
注意,在dev c++中可能无法执行atoi、atol和atof函数。