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. xaf 修改主页logo

    https://documentation.devexpress.com/#Xaf/CustomDocument3156

  2. VC++编写ActiveX控件

    ActiveX这门技术是通过生成“*.ocx”文件来实现的.先来了解下OCX文件,在百度百科上面对OCX是这样解释的:“.ocx是ocx控件的扩展名,OCX 是对象类别扩充组件.如果你用过Visual ...

  3. FreeMarker中的list集合前后台代码

    freemarker中的list集合前后台代码: FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页.电子邮件.配置文件.源代码等)的通用工具. 它 ...

  4. Linux下改动Matlab配置文件支持C++ 11标准以生成mex

    进入matlab 输入mex -v命令查看当前配置 输入命令改动配置文件 命令: !sudo gedit /usr/local/MATLAB/R2013a/bin/mexopts.sh 加入下面蓝色内 ...

  5. PL/SQL developer连接oracle出现“ORA-12154:TNS:could not resolve the connect identifier specified”问题的解决

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/50728536 本文出自[我是干勾鱼的博客] 使用PL/SQL developer ...

  6. 466. Count Linked List Nodes【Naive】

    Count how many nodes in a linked list. Example Given 1->3->5, return 3. 解法一: /** * Definition ...

  7. 开启Visual Studio 2013时,出现Microsoft.VisualStudio.Web.PasteJson.JsonPackage无法载入的可能解決方案

    1.先下载:http://www.jb51.net/dll/Microsoft.VisualStudio.Web.PasteJson.dll.html Microsoft.VisualStudio.W ...

  8. HTML中label的两种使用方法

    如果您在 label 元素内点击文本,就会触发此控件.就是说,当用户选择该标签时,浏览器就会自动将焦点转到和标签相关的表单控件上. 有两种使用方法: 方法1: <label for=" ...

  9. EmWebAdmin 生成流程分析

    继上一篇的简略的说明 EmWebAdmin 的地址以后下载,生成之后,这一篇讲一下该模板的生成流程 // 上一篇地址: http://www.cnblogs.com/chenfulin5/p/6856 ...

  10. LAMP 环境搭建关键步骤及注意事项

    一.安装MySQL1): 编译安装MySQL+----------------------------------------------------------------------------- ...