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

string s = "";
int a = lexical_cast<int>(s);
这种方法非常简单,笔者强烈建议大家忘掉std诸多的函数,直接使用boost:: lexical_cast。如果转换发生了意外,lexical_cast会抛出一个bad_lexical_cast异常,因此程序中需要对其进行捕捉。 现在动手 编写如下程序,体验如何使用boost:: lexical_cast完成数值转换。 【程序 -】使用boost:: lexical_cast完成对象数值转换 #include "stdafx.h" #include <iostream>
#include <boost/lexical_cast.hpp> using namespace std;
using namespace boost; int main()
{
string s = "";
int a = lexical_cast<int>(s);
double b = lexical_cast<double>(s); printf("%d/r/n", a + );
printf("%lf/r/n", b + ); try
{
int c = lexical_cast<int>("wrong number");
}
catch(bad_lexical_cast & e)
{
printf("%s/r/n", e.what());
} return ; }
如上程序实现字符串""到整数、双精度实数的转换(为了防止程序作弊,我们特意让它将值加1),结果输出如图4-19所示。

有了这个还用个毛atoi,itoa。

还有一种方法有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类型转换

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

  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. ros下boost移植

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

随机推荐

  1. C# : 连接SDE空间数据库时出错

    1.SDE认证问题:使用ArcGIS.KeyGen.exe生成一个epp文件方法:-->server-->All-->拷贝并保存为*.epp文件.认证时加载该文件即可认证完毕.2.实 ...

  2. C#实现DevExpress本地化实例详解

    using System; using System.Collections.Generic; using System.Text; using DevExpress.XtraGrid.Localiz ...

  3. Selenium 之18 种定位方式

    1 id 定位 driver.find_element_by_id() HTML 规定id 属性在HTML 文档中必须是唯一的.这类似于公民的身份证号,具有很强的唯一性 from selenium i ...

  4. python内置函数之print()

    定义:将值打印到一个流对象,或者默认打印到sys.stdout. 语法: print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=Fal ...

  5. jquery ajax 提交form表单 以及django后台接受

    HTML <form id="project_file_upload" enctype="multipart/form-data" > <di ...

  6. Shell脚本中调用另外一个脚本的方法

    (转载): 在Linux平台上开发,经常会在console(控制台)上执行另外一个脚本文件,经常用的方法有:./my.sh 或 source my.sh 或 . my.sh:这三种方法有什么不同呢?我 ...

  7. Groovy学习()Groovy是啥?

    Groovy是啥? groovy是英文中的一个单词,有marvelous.wonderful和excellen的意思. groovy是轻量级的,动态的,面向对象的,并且运行在JVM上的. groovy ...

  8. [systemd]Linux系统启动之systemd

    参照:https://wiki.debian.org/systemd 最近在添加板子应用程序自启动的时候,发现在rcN.d中的符号链接并没有用,文件系统为Debian Jessie 8, 后来从同事那 ...

  9. Tomcat Ajax跨域访问

    http://ibleave60.blog.51cto.com/2669415/1208652 http://enable-cors.org/server.html 下载cors-filter-1.7 ...

  10. ASP.NET 5 将于2016年一季度公布

    简单介绍:微软ASP.NET团队在GitHub宣布ASP.NET 5的公布时间表和发展蓝图. 该团队宣布在2015年还将公布三个Beta版.一个ASP.NET 5的抢先版(RC 1).到2016年一季 ...