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. unity, do nothing的state

    要想在animator的stateMachine中建一个"doNothing",要注意:为了保证"doNothing"state能正常运转,不被无故跳过, Mo ...

  2. js正则大扫除

    一:exec匹配不到后返回的是null reg = /c{2,}/; str='cainiao'; execReg(reg,str); 结果返回null,c{2,}表示2个以上的c,而cainiao中 ...

  3. POJ3187 Backward Digit Sums 【暴搜】

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4487   Accepted: 25 ...

  4. Python强制抛出自定义异常

    raise Exception("My Exception") 当程序运行到这行时,会抛出异常,打印出Exception: My Exception

  5. cocos2d-x onMouseMove中CCTouch *pTouch参数的细节

    /**************************************************************************** Copyright (c) 2010 coc ...

  6. (1)FluidMoveBehavior 之 ListBox 中详细内容项飞出来

    在 Blend 中集成了很多行为,首先需要了解一下Behaviors(行为)的几个关键点: (1)Behaviors(行为)是可复用代码集合,可以被任何对象附加使用: (2)设计人员和开发人员可以使用 ...

  7. Missing separate debuginfos, use: debuginfo-install

    环境:CentOS6.2 64位 操作:使用gdb调试C++查询MySQL数据库的程序 原因: 解决办法: 1.  修改文件/etc/yum.repos.d/CentOS-Debuginfo.repo ...

  8. python判断一个对象是否可迭代

    如何判断一个对象是可迭代对象? 方法是通过collections模块的Iterable类型判断: >>> from collections import Iterable >& ...

  9. Android中<uses-sdk>属性和target属性分析

    1. 概要 <uses-sdk> 用来描述该应用程序可以运行的最小和最大API级别,以及应用程序开发者设计期望运行的平台版本.通过在manifest清单文件中添加该属性,我们可以更好的控制 ...

  10. FreeRtos——多任务

    官方资料整理测试: 多任务和单任务几乎没有差别.只用多创建一个或多个任务,其他地方和单任务时相同. static void AppTaskCreate(void) { xTaskCreate(vTas ...