1,字符串 到 数值类型的转换

2,数值 到 字符串的转换 
3,异常处理情况 
4,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” 只能 “部分” 地转换为 123,不能 “完整” 地转换为 123.456,还是让我们需要适当的注意一些这两个模板参数就好了。 
  - 由于 Visual C++ 6 的本地化(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;
    int         a = 0;
    double        b = 0.0;
    std::string s = ""; 
    int            e = 0;    
    try
    { 
        // ----- 字符串 --> 数值 
        a = lexical_cast<int>("123");
        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 
    return 0;
}

boost.lexical_cast 学习的更多相关文章

  1. boost asio 学习(九) boost::asio 网络封装

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=10 9. A ...

  2. boost asio 学习(八) 网络基础 二进制写发送和接收

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=9 8. Net ...

  3. boost asio 学习(七) 网络基础 连接器和接收器(TCP示例)

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=8 7. Net ...

  4. boost库学习之开篇

    本系列文章使用boost_1.58.0版本. 一.欢迎使用boost C++库 boost致力于提供一个免费的.便携的源代码级的库. 我们重视那些与C++标准一起工作良好的库.boost库将要成为一个 ...

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

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

  6. boost asio 学习(一)io_service的基础

    原文  http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio/ 编译环境 b ...

  7. boost.asio学习-----reslover 域名解析

    将域名解析为ip地址并输出: #include "stdafx.h" #include "boost/asio.hpp" #include <boost/ ...

  8. boost::lexical_cast

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

  9. Boost::lexical_cast类型转换

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

随机推荐

  1. 读写锁ReadWriteLock

    为了提高性能,Java提供了读写锁,在读的地方使用读锁,在写的地方使用写锁,灵活控制,如果没有写锁的情况下,读是无阻塞的,在一定程度上提高了程序的执行效率. Java中读写锁有个接口java.util ...

  2. JAVA核心技术I---JAVA基础知识(package和import)

    一:package 所有的Java类都是放置在同一个目录下面的,因此类之间的相互调用无需显式声明调用. –同一个目录下,两个类的名字不能相同 –文件过多,查找和修改都不易,且容易出 Java支持多个目 ...

  3. 完美解决distinct中使用多个字段的方法

    众所周知,distinct可以列出不重复的记录,对于单个字段来说distinct使用比较简单,但是对于多个字段来说,distinct使用起来会使人发狂.而且貌似也没有见到微软对distinct使用多字 ...

  4. CSS-With-BEM

    Naming rules block_name__element_name--modifier_name-modifier_value Names are written in lowercase L ...

  5. Linux日志痕迹清除

    #coding=utf-8 import os import sys import subprocess def Clear_The_Log(host): logs = ["/var/log ...

  6. source命令导入大数据速度慢优化

    XX市邮政微商城的项目数据库,300多M,约220万条数据,source命令导入花了20个小时左右,太不可思议. 速度慢原因:220多万条数据,就 insert into 了220多万次,下图: 这是 ...

  7. LR与SVM的异同

    原文:http://blog.sina.com.cn/s/blog_818f5fde0102vvpy.html 在大大小小的面试过程中,多次被问及这个问题:“请说一下逻辑回归(LR)和支持向量机(SV ...

  8. 当一些库和类无法在Silverlight工程中使用

    因为要更新空间数据,所以需要导入MDB文件.但是Silverlight中不能直接使用OdbcConnection类读取MDB文件,所以需要新建一个ASP.NET空Web应用程序 工程 随后在此工程中, ...

  9. 关于PHP的加载类操作以及其他两种魔术方法应用

    <?php 加载类//include("./Ren.class.php");//include "./Ren.class.php";include_onc ...

  10. Docker 查看容器 IP 地址

    查看Docker的底层信息. docker inspect 会返回一个 JSON 文件记录着 Docker 容器的配置和状态信息 docker inspect NAMES # 查看容器所有状态信息: ...