在STL库中,我们可以通过stringstream来实现字符串和数字间的转换:

int i = 0;
    stringstream ss;

ss << "123";
    ss >> i;

但stringstream是没有错误检查的功能,例如对如如下代码,会将i给赋值为12.

ss << "12.3";
    ss >> i;

甚至连这样的代码都能正常运行:

ss << "hello world";
    ss >> i;

这显然不是我们所想要看到的。为了解决这一问题,可以通过boost::lexical_cast来实现数值转换:

int i = boost::lexical_cast<int>("123");
    double d = boost::lexical_cast<double>("12.3");

对于非法的转换,则会抛异常:

try
    {
        int i = boost::lexical_cast<int>("12.3");
    }
    catch (boost::bad_lexical_cast& e)
    {
        cout << e.what() << endl;
    }

对于16机制数字的转换,可以以如下方式进行:

template <typename ElemT>
    struct HexTo {
        ElemT value;
        operator ElemT() const {return value;}
        friend std::istream& operator>>(std::istream& in, HexTo& out) {
            in >> std::hex >> out.value;
            return in;
        }
    };

int main(void) 
    {
        int x = boost::lexical_cast<HexTo<int>>("0x10"); 
    }

用boost::lexical_cast进行数值转换的更多相关文章

  1. boost::lexical_cast

    boost::lexical_cast为数值之间的转换(conversion)提供了一揽子方案,比如:将一个字符串"转换成整数123,代码如下: "; int a = lexica ...

  2. boost常用库(一):boost数值转换

    在STL中有一些字符转换函数,例如atoi,itoa等,在boost里面只需用一个函数lexical_cast进行转换,lexical_cast是模板方法,使用时需要传入类型.只能是数值类型转字符串. ...

  3. boost/lexical_cast.hpp的简单使用方法_行动_新浪博客

    boost/lexical_cast.hpp的简单使用方法_行动_新浪博客     boost/lexical_cast.hpp的简单使用方法    (2010-03-19 16:31:13)    ...

  4. boost.lexical_cast 学习

    1,字符串 到 数值类型的转换 2,数值 到 字符串的转换 3,异常处理情况 4,boost::lexical_cast 的原型: template<typename Target, typen ...

  5. Boost::lexical_cast类型转换

    1.字符串->数值 C++代码 #include <boost/lexical_cast.hpp> #include <iostream> int main() { us ...

  6. Boost::Lexical_cast 的使用

    .C++代码 #include <boost/lexical_cast.hpp> #include <iostream> int main() { using boost::l ...

  7. [转] boost:lexical_cast用法

    转载地址:http://www.habadog.com/2011/05/07/boost-lexical_cast-intro/ 一.lexical_cast的作用lexical_cast使用统一的接 ...

  8. Javascript数值转换(string,int,json)

    数值: 在JavaScript中,数值转换一般有三种方式: 一.Number(param)函数:param可以用于任何数据类型 1.1 param是Boolean值,true和false分别转换为1和 ...

  9. javascript的数值转换

    在javascript中数值转换,最要的一点是函数第一个字母必须要大写.js中的函数有string字符型.number数值型.null空型.boolean布尔型.undefined未定义. 具体的转换 ...

随机推荐

  1. json模块、os模块

    一.eval模拟序列化操作 1.序列化 内存中的数据-------->转成一种中间格式(字符串)---------->存到文件中 dic={'name':'egon','age':18} ...

  2. 【python3.X】Scrapy学习途径参考

    如何爬取属性在不同页面的itemhttp://scrapy-chs.readthedocs.io/zh_CN/0.24/topics/request-response.html#topics-requ ...

  3. SpringCloud项目,接口调用返回http 500 - Internal Server Error的错误

    今天上班的时候,自己正在参与的Spring Cloud项目出现了问题,原本上周五还正常的项目突然所有接口调用都是返回http 500的错误. 项目的状态是在Eureka上可以看到对应微服务是在线状态, ...

  4. 通过py2exe打包python程序的过程中,解决的一系列问题

    py2exe的使用方法参考<py2exe使用方法>. 注:程序可以在解释器中正常运行,一切问题都出在打包过程中. 问题1: 现象:RuntimeError: maximum recursi ...

  5. How to enable download EXE files from the Sharepoint website

          As we all know,many applications have forbidden to upload and download exe files.Because the e ...

  6. MySQL源码中的String

    适用于离开作用域就销毁的字符串.

  7. 一些可能有点用处的C#开发经验

    前言: 下个月就要去进行Java开发了,以后C#碰的就少了(可惜去年买了三本C#的书,几乎还是全新的……),平时一些经验都记在OneNote里面,现在收集整理出来,因为只能利用交接工作的打酱油的时间, ...

  8. 『JavaScript』模仿接口

    JavaScript中并没有内置的创建或实现接口的方法.这里将利用JavaScript的灵活性,来实现与接口意义相同的功能. 什么是接口? 接口的好处: 接口提供了一种用以说明一个对象应该具有哪些方法 ...

  9. 剑指offer-跳台阶08

    题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). class Solution: def jumpFloor(self, ...

  10. Daily Scrum02 12.02

    今天是黑色星期一,虽然大家最近被各种大作业压得身心疲惫,但是团队的凝聚力战胜了一切不快. 看看同志们今天的战绩,是不是又有一种充实感油然而生呢??? By Ryan Mao who? Today? T ...