boost boost::lexical_castを使ってみる
あいさつ
どうも、はかせです。
今回はboost::lexical_castを使ってみます。
この記事ではboost_1_69_0を使っています。
boost::lexical_cast
boost::lexical_castは文字列→他の型、他の型→文字列の変換を行うキャストです。
「boost よく使う」とかでググるとよく見かけるやつです。
#include "pch.h" #include <iostream> #include <Windows.h> #include <boost/lexical_cast.hpp> using namespace std; using namespace boost; int main() { int testInt = 1234; double testDouble; float testFloat; string testString = "1.234"; try { testFloat = lexical_cast<float>(testString); cout << testFloat << endl; testDouble = lexical_cast<double>(testString); cout << testDouble << endl; auto test = lexical_cast<double>(testString); cout << test << endl; testString = lexical_cast<string>(testInt); cout << testString << endl; } catch(const boost::bad_lexical_cast& e) { cout << e.what() << endl; } getchar(); }
まぁ特段問題もなく変換することが出来ました。