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. mysql 快速生成删除数据库中所有的表的语句

    SELECT concat('DROP TABLE IF EXISTS ', table_name, ';') FROM information_schema.tables WHERE table_s ...

  2. Quartus II 中 Verilog 常见警告/错误汇总

    Verilog 常见错误汇总 1.Found clock-sensitive change during active clock edge at time <time> on regis ...

  3. golang反射举例

    反射就是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:并且能改变它的属性. package main import ( "f ...

  4. Centos7安装官方JDK

    一.下载jdk最新版本版本 链接地址:官方地址 二.上传jdk到centos下 三.检查当前linux系统上是否有jdk,linux命令:rpm -qa | grep java 查询结果: 卸载掉系统 ...

  5. js 格式化时间

    Date.prototype.format = function(format) { var date = { , "d+": this.getDate(), "h+&q ...

  6. SQL Server进阶(十一)临时表、表变量

    临时表 本地临时表 适合开销昂贵   结果集是个非常小的集合 -- Local Temporary Tables IF OBJECT_ID('tempdb.dbo.#MyOrderTotalsByYe ...

  7. 安装Rational Rose后提示java.lang.ClassNotFoundException

    1. 在运行中输入regedit回车,打开注册表编辑器2. 搜索 TrustedClasspath [REG_SZ] ,因为64位和32位不一样. 4.添加值c:\windows\java\trust ...

  8. 设置 img 在 div 中水平居中和垂直居中

    Ø  前言 写 html + css 时,img 标签的垂直与水平居中,应该是很常见的设计.实现的方式很多,但是容易遗忘,下面分别整理了几种实现方式. 1.   css 代码 <style ty ...

  9. zookeeper的作用

    1.管理(存储和读取)程序提交的状态数据 2.为用户程序提供数据节点监听服务

  10. Filter 起航 编程式配置 压缩响应 日志过滤器

    [编程式配置]可以用web.xml配置替换 @WebListenerpublic class FilterListenerConfigurator implements ServletContextL ...