boost::to_string
(在boost/exception/to_string.hpp
中找到)的作用是什麼?它與boost::lexical_cast<std::string>
和std::to_string
有什麼不同?std :: to_string,boost :: to_string和boost :: lexical_cast <std::string>有什麼區別?
回答
std::to_string
,自C++ 11起可用,專門用於基本數值類型。它也有一個std::to_wstring
變種。
其設計產生與std::sprintf
相同的結果。
您可以選擇此表單以避免依賴於外部庫/標頭。在
boost::lexical_cast<std::string>
作品可以插入std::ostream
,包括來自其他庫或你自己的代碼類型的任何類型。
優化專業化常見類型的存在,與通用的形式,類似於:
template< typename OutType, typename InType >
OutType lexical_cast(const InType & input)
{
std::stringstream temp_stream;
temp_stream << input;
OutType output;
temp_stream >> output;
return output;
}
您可以選擇這種形式的利用輸入類型的更大的靈活性泛型函數,或從類型產生std::string
你知道不是基本的數字類型。
boost::to_string
沒有直接記錄,並似乎是主要內部使用。其功能類似於lexical_cast<std::string>
,而不是std::to_string
。
還有更多的區別:助推器正常工作!請考慮以下代碼:
#include <limits>
#include <iostream>
#include "boost/lexical_cast.hpp"
int main()
{
double maxDouble = std::numeric_limits<double>::max();
std::string str(std::to_string(maxDouble));
std::cout << "std::to_string(" << maxDouble << ") == " << str << std::endl;
std::cout << "boost::lexical_cast<std::string>(" << maxDouble << ") == "
<< boost::lexical_cast<std::string>(maxDouble) << std::endl;
return 0;
}
$ ./to_string
std::to_string(1.79769e+308) == 179769313486231570814527423731704356798070600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
boost::lexical_cast<std::string>(1.79769e+308) == 1.7976931348623157e+308
請不要只是粘貼一些代碼。描述*你的代碼的作用和*它如何做。 – 2017-01-17 13:38:38
對於相同的double值,to_string()和lexical_cast()的輸出很簡單;-)) – 2017-01-17 15:28:18
這些結果當然不同 - 值得注意。但是,對我而言,哪個結果更「正確」並不明顯。 – 2017-01-17 21:10:15
- 1. boost :: lexical_cast和std :: to_string對於無符號字符的正確結果是什麼
- 2. boost :: lexical_cast <>
- 3. stoi和std :: to_string on mingw 4.7.1
- 4. Boost lexical_cast <std::string>(int)MSVC 2008 bug
- 5. boost :: static_pointer_cast vs boost :: shared_static_cast有什麼區別?
- 6. 結合的boost :: lexical_cast的和std ::變換
- 7. System :: Convert :: ToString()和std :: to_string()之間的區別?
- 8. boost :: io_service poll_one和run_one有什麼區別?
- 9. 爲什麼數字類型只有`to_string()`?
- 10. 爲什麼使用std :: thread :: hardware_concurrency()和boost :: thread :: hardware_concurrency()會有區別?
- 11. Boost智能指針和std智能指針有什麼區別?
- 12. 可以boost :: lexical_cast <std::string>(Int_Type)有沒有扔?
- 13. 如何創建自定義boost :: posix_time to_string formaters?
- 14. std :: to_string()無法解析
- 15. 'to_string'不是'std'的成員
- 16. boost :: bind和boost :: lambda :: bind有什麼區別?
- 17. boost :: signals2 :: mutex和boost :: thread :: mutex有什麼區別?
- 18. Boost :: bind和Boost Phoenix :: bind有什麼區別?
- 19. 如何使用boost :: lexical_cast和std :: boolalpha?即升壓:: lexical_cast的< bool >(「真」)
- 20. to_string問題
- 21. boost :: lexical_cast帶正號
- 22. std :: array和boost :: array之間的區別
- 23. boost :: bind與地圖綁定,綁定std :: pair和std :: map :: value_type有什麼區別?
- 24. boost :: lexical_cast和雙重奇怪行爲
- 25. 帶模板的C++ boost lexical_cast?
- 26. 模仿boost lexical_cast操作
- 27. 執行身份驗證boost :: lexical_cast有什麼開銷?
- 28. in boost iostream filtering_ostream,sync(),strict_sync()和flush()之間有什麼區別?
- 29. std,tr1和boost(作爲名稱空間和/或庫)有什麼區別?
- 30. boost :: lexical_cast <int>(「-138.8468953457983248」)拋出異常
如果我正確記得歷史記錄,'boost :: to_string'在'std :: to_string'之前,就像通常情況一樣。在它被接納到標準庫之前,Boost往往是東西的遊樂場。 'std :: to_string'是C++ 11中新增的。 – CoryKramer 2015-04-01 19:21:43
@Cyber:我建議你把它寫成(部分)答案。 – MikeMB 2015-04-01 19:40:00
@CoryKramer,'boost :: to_string'可能會更老,但它不同於'std :: to_string'和'std :: to_string'不是基於它(它們恰好使用相同的名稱)。 – 2017-01-17 13:58:44