boost::lexical_cast:用于类型转换,将string转为整形,或者整形转为string类型
头文件:
#include <boost/lexical_cast.hpp>
参考代码:
#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
using namespace std;
int main()
{
try
{
string s_id = "1.11";
double d_number = boost::lexical_cast<double>(s_id);
size_t int_m = 100;
string string_m = boost::lexical_cast<string>(int_m);
cout<<d_number<<endl;
cout<<string_m<<endl;
}
catch(boost::bad_lexical_cast&e)
{
cout<<"error:"<<e.what()<<endl;
}
return 0;
}
将string转为double类型,将size_t类型转为string类型,如果转换失败,会抛出异常,在catch中捕获。