Boost lexical_cast[转]
#include <iostream> #include <iomanip> #include <sstream> #include<boost/lexical_cast.hpp> using namespace std; int main() { using boost::lexical_cast; using boost::bad_lexical_cast; try { //字符串 转 整型 //int a = lexical_cast<int>("123"); int a = lexical_cast<int>("123efd", 3); cout << a << endl; //字符串 转 浮点型 float b = lexical_cast<float>("1.23456"); cout << b << endl; //浮点数 转 为字符串 float float_c = 1.23456f; string c = lexical_cast<string>(float_c); cout << c << endl; //整数转为字符串 int int_d = 12345; string d = lexical_cast<string>(int_d); cout << d << endl; } catch (const bad_lexical_cast & e) { cout << e.what() << endl; } #if 0 double dble = 123456.7893232338; std::stringstream ss; ss.precision(20); ss << setw(15) << dble; string ssd = ss.str(); cout << ssd << endl; #endif return 0; }