1.整数と浮動小数点間の変換
(1)一般的な必須変換ですが、問題があります
double d_Temp; int i_Temp = 323 ; d_Temp =(double)i_Temp;
(2)標準の強制変換を使用する
とりあえず、static_cast <type>(変換された値)、4つの標準必須タイプがありますが、それほど具体的ではありません。
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に文字列を変換するために、ダブル、フロート、長い長い 、長い二種類
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を使用して文字*から文字*を変換する
string s_temp = " これは文字列です" ; const char * c_temp; c_temp = s_temp.c_str();
(2)追加機能を使用して、文字を1つずつ追加できます。
文字列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 ] = ' \ 0 ' ; s_temp = c_temp;