、C++代码
#include <boost/lexical_cast.hpp>
#include <iostream>
int main()
{
        using boost::lexical_cast;
        ");
        double b = lexical_cast<double>("123.12");
         std::cout<<a<<std::endl
         std::cout<<b<<std::endl;
        ;
}  

、数值->字符串

C++代码
#include <boost/lexical_cast.hpp>
#include <string>
#include <iostream>
int main()
{
        using std::string;
        const double d = 123.12;
         string s = boost::lexical_cast<string>(d);
         std::cout<<s<<std::endl;
        ;
}  

、异常

C++代码
#include <boost/lexical_cast.hpp>
#include <iostream>
int main()
{
        using std::cout;
        using std::endl;
        int i;
        try
         {
             i = boost::lexical_cast<int>("xyz");
         }
        catch(boost::bad_lexical_cast& e)
         {
             cout<<e.what()<<endl;
            ;
         }
         cout<<i<<endl;
        ;
}  

显然“xyz”并不能转换为一个int类型的数值,于是抛出异常,捕捉后输出“bad lexical cast: source type value could not be interpreted as target”这样的信息。

、注意事项

lexical_cast依赖于字符流std::stringstream,其原理相当简单:把源类型读入到字符流中,再写到目标类型中,就大功告成。

C++代码
");  

相当于

C++代码
int d;
std::stringstream s;
s<<";
s>>d;  

、boost::lexical_cast 的原型:
template<typename Target, typename Source>
    Target lexical_cast(Source arg);
lexical_cast 是依赖于字符串流 std::stringstream 的,其原理也是相当的简单:把源类型 (Source) 读入到字符流中,再写到目标类型 (Target) 中。但这里同时也带来了一些限制:
  - 输入数据 (arg) 必须能够 “完整” 地转换,否则就会抛出 bad_lexical_cast 异常。例如:
   int i = boost::lexical_cast<int>("123.456"); // this will throw
    因为 “,不能 “完整” 地转换为 123.456,还是让我们需要适当的注意一些这两个模板参数就好了。
  - 由于 Visual C++  的本地化(locale)部分实现有问题,如果使用了非默认的 locale,可能会莫名其妙地抛出异常。
  - 源类型 (Source) 必须是一个可以输出到输出流的类型(OutputStreamable),也意味着该类型需要 operator<< 被定义。
  - 同样的,目标类型 (Target) 必须是一个可以输入到输入流的类型 (InputStreamable),也意味着该类型需要 operator>> 被定义。
  - 另外,Both Source and Target are CopyConstructible。
  - Target is DefaultConstructible。
其中,下面的四个限制是在使用自定义类型之间转换时必须做的一些工作的(当然流行的使用的 C 库函数等等都是不可以处理自定义类型之间的转换的)。

#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
#define ERROR_LEXICAL_CAST     1
int main()
{
    using boost::lexical_cast;
    ;
    double        b = 0.0;
    std::string s = "";
    ;
    try
    {
        // ----- 字符串 --> 数值
        a = lexical_cast<");
        b = lexical_cast<double>("123.12");
        // ----- 数值 --> 字符串
        s = lexical_cast<std::string>("123456.7");
        // ----- 异常处理演示
        e = lexical_cast<int>("abc");
    }
    catch(boost::bad_lexical_cast& e)
    {
        // bad lexical cast: source type value could not be interpreted as target
        std::cout << e.what() << std::endl;
        return ERROR_LEXICAL_CAST;
    } 

    std::cout << a << std::endl;    // 输出:123
    std::cout << b << std::endl;    // 输出:123.12
    std::cout << s << std::endl;     // 输出:123456.7
    ;
}

、小结

我们已经体验了boost::lexcial_cast。当然,lexical_cast不仅仅局限于字符串类型与数值类型之间的转换:可在任意可输出到stringstream的类型和任意可从stringstream输入的类型间转换。

一、lexical_cast的作用
lexical_cast使用统一的接口实现字符串与目标类型之间的转换。

二、lexical_cast与c/c++提供类似接口的比较
标准c家族中包含此类函数,例如atoi与itoa等,它们的缺点是:
()各个转换都是单向的,双向转换为不同函数,各种转换函数不同,接口众多;
()仅支持基础数据类型的子集,如int,long,double;
()不能提供统一的接口,易用性差;

c++中提供了stringstream,使用它进行格式转换可读性较差,使用起点较高,只是简单的转换,stringstream太重量级。

boost提供了lexical_cast,使用统一接口形式实现任意类型之间的转换,增强了易用性。但如果需要严密控制精度的转换,仍然推荐使用stringstream;数值之间的转换,推荐使用numeric_cast。

三、lexical_cast的样例
#include "iostream"
#include "boost/lexical_cast.hpp" // 需要包含的头文件
 
using boost::lexical_cast;
using boost::bad_lexical_cast;
using namespace std;
 
int main()
{
    ";
    ;
    try
    {
        i=lexical_cast<int>(p); // 将字符串转化为整数
    }
    catch(bad_lexical_cast&)    // 转换失败会抛出一个异常
    {
        i=;
    }
    cout << i << endl;
    return i;
}

Boost::Lexical_cast 的使用的更多相关文章

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

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

  2. boost.lexical_cast 学习

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

  3. boost::lexical_cast

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

  4. Boost::lexical_cast类型转换

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

  5. 用boost::lexical_cast进行数值转换

    在STL库中,我们可以通过stringstream来实现字符串和数字间的转换: int i = 0;    stringstream ss; ss << "123";  ...

  6. [转] boost:lexical_cast用法

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

  7. boost之lexical_cast

    第一次翻译,虽然是个很简单的函数介绍... 文件boost/lexical_cast.hpp中定义了此函数: namespace boost { class bad_lexical_cast; tem ...

  8. boost 学习笔记 1: lexical_cast

    参考原著地址:http://einverne.github.io/post/2015/12/boost-learning-note-1.html 转换对象要求 lexical_cast 对转换对象有如 ...

  9. ros下boost移植

    参考资料: http://blog.chinaunix.net/uid-12226757-id-3427282.html  注意:本链接中只看第一种的方法,验证程序参考以下: Boost安装成功的验证 ...

随机推荐

  1. C# 笔记——数据类型

    一张图读懂C#数据类型:

  2. python库-urllib

    urllib库提供了一系列操作url的功能,是python处理爬虫的入门级工具,网上的学习资料也很多.我做爬虫是一开始就用了Scrapy框架,并不是一步步从urllib开始的,反而是在后来解决一些小问 ...

  3. MySQL关键字大全

    转载自:https://blog.csdn.net/benxiaohai888/article/details/77803090 在使用MySQL的时候,一般尽量避免用关键字作为表名,如使用关键字做表 ...

  4. NativeScriptEngineService 被调用流程

    AbsractSearchScritp 有个lookup! NativeScriptEngineService search()会调用 script.setLookup() NativeScriptE ...

  5. 爬虫基础库之requests

    requests Python标准库中提供了:urllib.urllib2.httplib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作, ...

  6. centos7 安装rlwrap

    https://blog.csdn.net/zhjmozhi/article/details/78347216

  7. 前端自动化gulp遇上es6从 无知到深爱

    Gulp是什么? Gulp是前端自动化的工具,但Gulp能用来做什么 1.搭建web服务器 2.使用预处理器Sass,Less 3.压缩优化,可以压缩JS CSS Html 图片 4.自动将更新变化的 ...

  8. 五十四 网络编程 TCP编程

    Socket是网络编程的一个抽象概念.通常我们用一个Socket表示“打开了一个网络链接”,而打开一个Socket需要知道目标计算机的IP地址和端口号,再指定协议类型即可. 客户端 大多数连接都是可靠 ...

  9. php 正则验证字符串是否为数字

    PHP 正则验证字符串是否为数字 方法一: php中利用正则表达式验证字符串是否为数字一件非常容易的事情,最主要的是如何写好正则表达式以及掌握正则表达式的写法,在此利用正则表达式的方式来列举一下判断数 ...

  10. HDU 6467.简单数学题-数学题 (“字节跳动-文远知行杯”广东工业大学第十四届程序设计竞赛)

    简单数学题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...