boost::lexical_cast
- 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的更多相关文章
- boost/lexical_cast.hpp的简单使用方法_行动_新浪博客
boost/lexical_cast.hpp的简单使用方法_行动_新浪博客 boost/lexical_cast.hpp的简单使用方法 (2010-03-19 16:31:13) ...
- boost.lexical_cast 学习
1,字符串 到 数值类型的转换 2,数值 到 字符串的转换 3,异常处理情况 4,boost::lexical_cast 的原型: template<typename Target, typen ...
- Boost::lexical_cast类型转换
1.字符串->数值 C++代码 #include <boost/lexical_cast.hpp> #include <iostream> int main() { us ...
- Boost::Lexical_cast 的使用
.C++代码 #include <boost/lexical_cast.hpp> #include <iostream> int main() { using boost::l ...
- 用boost::lexical_cast进行数值转换
在STL库中,我们可以通过stringstream来实现字符串和数字间的转换: int i = 0; stringstream ss; ss << "123"; ...
- [转] boost:lexical_cast用法
转载地址:http://www.habadog.com/2011/05/07/boost-lexical_cast-intro/ 一.lexical_cast的作用lexical_cast使用统一的接 ...
- boost之lexical_cast
第一次翻译,虽然是个很简单的函数介绍... 文件boost/lexical_cast.hpp中定义了此函数: namespace boost { class bad_lexical_cast; tem ...
- boost 学习笔记 1: lexical_cast
参考原著地址:http://einverne.github.io/post/2015/12/boost-learning-note-1.html 转换对象要求 lexical_cast 对转换对象有如 ...
- ros下boost移植
参考资料: http://blog.chinaunix.net/uid-12226757-id-3427282.html 注意:本链接中只看第一种的方法,验证程序参考以下: Boost安装成功的验证 ...
随机推荐
- Redis总结(七)Redis运维常用命令(转载)
redis 服务器端命令 redis 127.0.0.1:6380> time ,显示服务器时间 , 时间戳(秒), 微秒数 1) "1375270361" 2) &quo ...
- C++:SQLServer字段赋值
大前提: 1. 初始化环境 2. 创建连接实例 3. 创建记录集实例 注意点: 1.AddNews失败问题: (1)是否将CursorTypeEnum cursorType, LockTypeEnum ...
- C编程测试存储格式为大段还是小段
目前,计算机存储系统有2种存储格式,大端和小端.数据在内存中存储时以字节为单位,一个int类型有4个字节,这就导致是高字节对应低地址(大端模式),高字节对应高地址(小端模式).大端和小端模式本身没有对 ...
- php排序函数学习
sort() 函数按升序对给定数组的值排序. 注释:本函数为数组中的单元赋予新的键名.原有的键名将被删除. 如果成功则返回 TRUE,否则返回 FALSE. <?php$my_array = a ...
- 11个常用的Linux命令
Linux命令行吸引了大多数Linux爱好者.一个正常的Linux用户一般掌握大约50-60个命令来处理每日的任务.今天为你解释下面几个命令:sudo.python.mtr.Ctrl+x+e.nl.s ...
- 安卓-启动下一个Activity
Intent intent = new Intent(NotebookActivity.this, MessageActivity.class); startActivity(intent); 通过意 ...
- JAVA多线程之synchronized和volatile实例讲解
在多线程中,提到线程安全.线程同步,我们经常会想到两个关键字:volatile和synchronized,那么这两者有什么区别呢? 1. volatile修饰的变量具有可见性 volatile是变量修 ...
- Struts2源码阅读(一)_Struts2框架流程概述
1. Struts2架构图 当外部的httpservletrequest到来时 ,初始到了servlet容器(所以虽然Servlet和Action是解耦合的,但是Action依旧能够通过httpse ...
- libjpeg.a exists or that its path is correct
Android NDK: ERROR:/cygdrive/e/cocos2d-x/code/cocos2d-2.1rc0-x-2.1.3/HelloTest1/proj.android/../../c ...
- java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明)
转载地址:http://www.devba.com/index.php/archives/4581.html java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明); ...