C++ 中 string int/double /float 互相轉換

前提:MFC中才有CString 而標準C++中只有string

1.標準C++ 其他類型 轉string:
int x = 0;
string str =to_string(x);

string轉其他: 利用stringstream
float str2float(string num)
{
float res;
stringstream stream(num);
stream>>res;
return res;
}

2。對於unicode編碼來說,CString 轉其他類型可以用_ttof _ttoi 等
_tstof
float x;

3.對於ansi來說,使用atoi atof 由CString轉其他類型

添加鏈接描述

下面文章中對於mfc不能使用atof問題做了講解
https://blog.csdn.net/lhw19931201/article/details/90178499

我的工程使用的是unicode 字符集,而atof是ansi定義的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章