1 //强制类型转换 2 #include "stdafx.h" 3 #include <iostream> 4 #include <string> 5 #include <boost/lexical_cast.hpp> 6 7 using namespace std; 8 9 int _tmain(int argc, _TCHAR* argv[]) 10 { 11 string str_int="520"; 12 string str_double="520.2314"; 13 const double d = 124.45; 14 try 15 { 16 cout << boost::lexical_cast<int>(str_int) << endl; 17 cout << boost::lexical_cast<double>(str_double) << endl;//默认精度为6 18 cout << boost::lexical_cast<string>(d) << endl; 19 } 20 catch(boost::bad_lexical_cast& e) 21 { 22 cerr<<"Bad cast:"<<e.what()<<endl; 23 } 24 return 0; 25 }