[转] boost:lexical_cast用法
转载地址:http://www.habadog.com/2011/05/07/boost-lexical_cast-intro/
一、lexical_cast的作用
lexical_cast使用统一的接口实现字符串与目标类型之间的转换。
二、lexical_cast与c/c++提供类似接口的比较
标准c家族中包含此类函数,例如atoi与itoa等,它们的缺点是:
(1)各个转换都是单向的,双向转换为不同函数,各种转换函数不同,接口众多;
(2)仅支持基础数据类型的子集,如int,long,double;
(3)不能提供统一的接口,易用性差;
c++中提供了stringstream,使用它进行格式转换可读性较差,使用起点较高,只是简单的转换,stringstream太重量级。
boost提供了lexical_cast,使用统一接口形式实现任意类型之间的转换,增强了易用性。但如果需要严密控制精度的转换,仍然推荐使用stringstream;数值之间的转换,推荐使用numeric_cast。
三、lexical_cast的接口形式
|
1
2
3
|
template<typename target,="" typename="" source="">Target lexical_cast(const Source& arg);</typename> |
四、lexical_cast的样例代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#include "iostream"#include "boost/lexical_cast.hpp" // 需要包含的头文件using boost::lexical_cast;using boost::bad_lexical_cast;using namespace std;int main(){ char* p="32768"; int i=0; try { i=lexical_cast<int>(p); // 将字符串转化为整数 } catch(bad_lexical_cast&) // 转换失败会抛出一个异常 { i=0; } cout << i << endl; return i;}</int> |
[转] 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
boost::lexical_cast为数值之间的转换(conversion)提供了一揽子方案,比如:将一个字符串"转换成整数123,代码如下: "; int a = lexica ...
- 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之boost::share_ptr用法
boost中提供了几种智能指针方法:scoped_ptr shared_ptr intrusive_ptr weak_ptr,而标准库中提供的智能指针为auto_ptr. 这其中,我最喜欢,使用最多的 ...
- [转] boost::function用法详解
http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost/fun ...
- boost::thread用法
最近在做一个消息中间件里面涉及到多线程编程,由于跨平台的原因我采用了boost线程库.在创建线程时遇到了几种线程创建方式现总结如下: 首先看看boost::thread的构造函数吧,boost::th ...
随机推荐
- smarty模板引擎基础(二)
smarty模板引擎所需文件夹共可分为存放页面缓存的(cache).存放配置文件的(configs).存放模板扩充插件的(plugins).存放模板文件的(templates).存放编译后的模板文件的 ...
- World Finals 2017 (水题题解)
看大佬做2017-WF,我这种菜鸡,只能刷刷水题,勉强维持生活. 赛后补补水题. 题目pdf链接,中文的,tls翻译的,链接在这里 个人喜欢在vjudge上面刷题. E Need for Speed ...
- codeforces 702B B. Powers of Two(水题)
题目链接: B. Powers of Two time limit per test 3 seconds memory limit per test 256 megabytes input stand ...
- 自然语言处理:问答 + CNN 笔记
参考 Applying Deep Learning To Answer Selection: A Study And An Open Task follow: http://www.52nlp.cn/ ...
- 用C++发邮件
近段时间,实验室电脑的IP频繁地改变,搞得想用远程偷下懒都不行.这时想到的解决方法有:静态IP,动态域名,自己解决.静态IP虽然可以自己指定,但一关机后,与对方冲突就完了,作罢.免费的动态域名又要手机 ...
- ASP.NET:Global.asax
ylbtech-.Net-ASP.NET:Global.asax 1.返回顶部 1. 一.定义:Global.asax 文件(也称为 ASP.NET 应用程序文件)是一个可选的文件,该文件包含响应 A ...
- glance image-create
glance image-create
- 华为G700电脑版Root软件-Eroot
华为G700电脑版Root软件--eroot.zip(10.5M) 查阅全文 ›
- 1.SJ-SLAM-14
1.引言 SLAM:Simultaneous Localization and Mapping 同时定位与地图构建 搭载特定传感器的主体,在没有环境先验信息的情况下,于运动过程中建立环境的模型,同时估 ...
- WPF后台通知前台事件(ViewModelBase记录)
版本1: 定义:public class ModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler P ...