實戰準標準庫Boost —— (2)測試Boost配置的Hello World程式

鍾超發表於2012-01-08

1. 配置環境

請先按照《Boost C++ Libs —— (1)配置Boost的VS2008開發環境》一文在Visual Studio中配置開發環境。


2. 原始碼

#include <boost/lexical_cast.hpp>  
#include <iostream>  

using namespace std;

int main()
{
	using boost::lexical_cast; 

	int a=lexical_cast<int>("123"); 
	double b=lexical_cast<double>("123.0123456789"); 
	string s0=lexical_cast<string>(a); 
	string s1=lexical_cast<string>(b); 
	cout<<"number: "<<a<<"  "<<b<<endl; 
	cout<<"string: "<<s0<<"  "<<s1<<endl; 
	int c=0; 
	try{  
		c=lexical_cast<int>("abcd"); 
	}  
	catch(boost::bad_lexical_cast& e){  
		cout<<e.what()<<endl; 
	}

	return 0; 
}

相關文章