C++11
头文件:#include <string>
数值转String
to_string(val):可以将其他类型转换为string。
String转数值
s:表示整数内容的字符串;
b:(非必须)表示转换所用的基数,默认为10(表示十进制);
p:(非必须)是size_t的指针,用来保存s中第一个非数值字符的下标,p默认为0,即函数不返回下标。
stoi(s, p, b):string转int
stol(s, p, b):string转long
stod(s, p, b):string转double
stof(s, p, b):string转float
stold(s, p, b):string转long dluble
stoul(s, p, b), stoll(s, p, b), stoull(s, p, b)等。
void testTypeConvert()
{
//int --> string
int i = 5;
string s = to_string(i);
cout << s << endl;
//double --> string
double d = 3.14;
cout << to_string(d) << endl;
//long --> string
long l = 123234567;
cout << to_string(l) << endl;
//char --> string
char c = 'a';
cout << to_string(c) << endl; //自动转换成int类型的参数
//char --> string
string cStr; cStr += c;
cout << cStr << endl;
s = "123.257";
//string --> int;
cout << stoi(s) << endl;
//string --> long
cout << stol(s) << endl;
//string --> float
cout << stof(s) << endl;
//string --> doubel
cout << stod(s) << endl;
}