lexical_cast定义在boost命名空间,使用时须要包含头文件<boost/lexical_cast.hpp>.ios
lexical_cast由一组模版组成,不一样的模版对应不一样的数据类型转换。下面列出主要的两种。函数
template <typename Target, typename Source> inline Target lexical_cast(const Source &arg) { Target result = Target(); if (!boost::conversion::detail::try_lexical_convert(arg, result)) { boost::conversion::detail::throw_bad_cast<Source, Target>(); } return result; } template <typename Target> inline Target lexical_cast(const char* chars, std::size_t count) { return ::boost::lexical_cast<Target>( ::boost::iterator_range<const char*>(chars, chars + count) ); }
int x = boost::lexical_cast<int>("100"); //字符串->整型 100 long y = boost::lexical_cast<long>("2000"); //字符串->长整型 2000 float pai = boost::lexical_cast<float>("3.14159"); //字符串->符点型 3.14159 double e = boost::lexical_cast<double>("2.71828"); //字符串->双精度符点型 2.71828 double r = boost::lexical_cast<double>("1.414abcdefg", 5); //C字符串->双精度符点型(只转前5位) 1.141 std::string str = boost::lexical_cast<std::string>(0.618); //双精度符点型->字符串 0.61799999999999999 std::string str2 = boost::lexical_cast<std::string>(0x10); //16进制数->字符串 16
注:字符串转数字时,字符串中不能包含除数字之外的字符(表示指数的e/E除外)。例如,不能将"123L"、"0x100"这样的字符串转换成数字。测试
当lexical_cast没法执行转换操做时会抛出异常boost::bad_lexical_cast。spa
lexical_cast在命名空间boost::conversion提供了不抛出异常的版本。.net
template <typename Target, typename Source> inline bool try_lexical_convert(const Source& arg, Target& result) template <typename Target, typename CharacterT> inline bool try_lexical_convert(const CharacterT* chars, std::size_t count, Target& result)
该系列模板能够用来测试是否能够转换成功。code
lexical_cast 对转换对象有以下要求:对象
注:C++语言中的内建类型(int,double等)和std::string都知足以上的三个条件。对于自定义类,须要按照这三个条件进行分别实现。blog
最主要的是定义输入输出操做符,同时可以使用缺省构造函数和拷贝构造函数。字符串
#include <iostream> #include <boost/lexical_cast.hpp> class DemoClass { public: std::string str; }; //重载流输出操做符,可用于转换的起点 std::ostream &operator << (std::ostream &os, const DemoClass &obj) { os << obj.str; return os; } //重载流输入操做符,可用于转换的终点 std::istream &operator >> (std::istream &is, DemoClass &obj) { is >> obj.str; return is; } int main() { DemoClass demoObj = boost::lexical_cast<DemoClass>("HelloWorld"); //demoObj.str = HelloWorld std::string demoStr = boost::lexical_cast<std::string>(demoObj); //demoStr = HelloWorld }
注:这里转换过程当中若是字符串中间出现空格好比"Hello World",将致使转换失败。缘由及解决办法后续提供。get
C++11标准加强了字符串与数字的互操做性。新标准提供了一系列函数
字符串转数字
数字转字符串
使用示例
assert(std::stoi(" 42 ") == 42); //转换int,容许有空格 assert(std::stol("100L") == 100L); //转换long, 支持L等后缀 assert(std::stol("1000 9") == 1000L); //转换long, 后面的被忽略 assert(std::stod("3.14ispi") == 3.14); //转换double, 遇到无效的字符中止 assert(std::to_string(776ul) == "776"); //转换为string
与boost::lexical_cast相比。C++11标准容许字符串里出现非数字字符--会忽略起始的白空格,直到遇到没法转换的字符为止。
缘由分析:http://blog.csdn.net/ugg/article/details/3381138