1、整型和浮点型之间的转换
(1)普通的强制转换,但是存在问题
double d_Temp; int i_Temp = 323; d_Temp = (double)i_Temp;
(2)使用标准的强制转换
暂时知道static_cast<类型>(转换的值);标准的强制类型有四种,具体还不太会。
double d_Temp; int i_Temp = 323; d_Temp = static_cast<double>(i_Temp);
2、字符串和数字之间的转换,最强大的还是atoi类的
(1)用sprintf_s函数将数字转换成字符串
int i_temp = 2020; std::string s_temp; char c_temp[20]; sprintf_s(c_temp, "%d", i_temp); s_temp = c_temp; std::cout << s_temp << std::endl;
(2)用sscanf函数将字符串转换成数字
double i_temp; char c_temp[20] = "15.234"; sscanf_s(c_temp, "%lf", &i_temp); std::cout << i_temp << std::endl;
(3)atoi, atof, atol, atoll(C++11标准) 函数将字符串转换成int,double, long, long long 型
std::string s_temp; char c_temp[20] = "1234"; int i_temp = atoi(c_temp); s_temp = c_temp;
std::string s_temp; char c_temp[20] = "1234.1234"; double i_temp = atof(c_temp); s_temp = c_temp;
(4)strtol, strtod, strtof, strtoll,strtold 函数将字符串转换成int,double,float, long long,long double 型
std::string s_temp; char c_temp[20] = "4.1234"; double a = strtod(c_temp, nullptr); //后面的参数是如果遇到字符串则是指向字符串的引用
(5)用to_string把数字转化成字符串
double d_temp = 1.567; std::string s_temp = to_string(d_temp);
3、字符串和字符之间的转换
(1)用c_str从字符串转换char*
string s_temp = "this is string"; const char* c_temp; c_temp = s_temp.c_str();
(2)把字符一个一个加进去可以用append函数
string s_temp; char c_temp[20]; c_temp[0] = 'H'; c_temp[1] = 'L'; c_temp[2] = 'L'; c_temp[3] = 'O'; c_temp[4] = '