1、字符串->数值

C++代码

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

2、数值->字符串

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;
return ;
}

3、异常

  如果转换发生了意外,lexical_cast会抛出一个bad_lexical_cast异常,因此程序中需要对其进行捕捉。

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;
return ;
}
cout<<i<<endl;
return ;
}

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

4、注意事项

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

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 的使用

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

  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. CC++初学者编程教程(3) 安装VS2010 boost标准库开发环境

    1.      BOOST编译过程非常复杂,目前为了学习BOOST,首先搭建基于VS2010的BOOST开发环境. Boost库 8 9. 10. 11 12 13 14 15. 16. 17. 18 ...

随机推荐

  1. JVM内存占用情况深入分析

    内存分布 首先,列举一下一个JVM进程主要占用内存的一些地方: Young Old metaspace java thread count * Xss other thread count * sta ...

  2. python学习 (二十九) range函数

    1:list函数可以将其他类型转成list. print(list(range(0, 10))) 2: list函数把元组转成list t = (1, 3, 3, 5) print(list(t)) ...

  3. js内置对象中获取时间的用法--通过date对象获取。

    Date对象: var today = new Date(); //年份: var year = today.getFullYear(); //月份 var month = today.getMont ...

  4. 【BZOJ】2743: [HEOI2012]采花(树状数组)

    题目 传送门:QWQ 分析 已经凉凉.看错数据范围敲了发莫队........ 和HH的项链差不多,把每种颜色之前的颜色到再之前的颜色这段区间 区间加. 区间加就树状数组特技 代码 #include & ...

  5. 缓存varnish的管理及配置详解

    一 工作原理 在当前主流的Web服务架构体系中,Cache担任着越来越重要的作用.常见的基于浏览器的C/S架构,Web Cache更是节约服务器资源的关键.而最近几年由FreeBSD创始人之一Kamp ...

  6. Whoops, looks like something went wrong

    Whoops, looks like something went wrong. 这是由于访问laravel项目报错的,解决几种可能出现的错误. 1)打开:D:\java\wamp\www\subwa ...

  7. OD 界面简介

    1 为汇编代码对应的地址窗口 2 为汇编代码对应的十六进制机器码窗口 3 为反汇编窗口 4 为反汇编代码对应的注释信息窗口 5 为寄存器信息窗口 6 为当前执行到的反汇编代码的信息窗口 7~9 为数据 ...

  8. OpenStack--Cinder(G版)中的volume type

    一.volume type的相关操作 Cinder中的卷类型,是卷的一种标识,各个OpenStack的发行者可根据自身对系统的约束来定义卷类型的使用.G版的Cinder中与卷类型相关的两种资源:typ ...

  9. mac 使用svn记录

    checkout  project : svn checkout svn://127.0.0.1/repository --username=username --password=password ...

  10. PHP根据问题追踪代码技巧一

    1.问题描述: 2.E:\html\pim\php_aspire-mcloud\module\pim\controller\Configure.class.php public function po ...