[转] 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 ...
随机推荐
- void类型和void *指针类型(网上摘抄总结)【转】
http://www.blogjava.net/fhtdy2004/archive/2009/07/09/286004.html 现在在学linux编程过程中遇到很多void *指针类型,由于c很早学 ...
- js事件传播的一个疑惑
在学习事件传播的时候,发现一个问题,当时是这样子的. 我给多层元素分别绑定了冒泡和捕获事件.按道理应该先从外向内执行完所有的捕获事件,再由内向外执行所有的冒泡事件. 但是天不随人愿啊,有个元素偏偏先执 ...
- 【CQ18高一暑假前挑战赛1】标程
[A] #include<bits/stdc++.h> using namespace std; #define ll long long ll qpow(ll a,ll x,ll Mod ...
- [BZOJ 1475] 方格取数
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1475 [算法] 首先将方格黑白染色 , 也就是说 , 如果(i + j)为奇数 , ...
- Exception in thread “main” java.sql.SQLException: The server time zone value ‘�й���ʱ��’ is unrecognized or represents more than one time zone.
Exception in thread “main” java.sql.SQLException: The server time zone value ‘�й���ʱ��’ is unrecogn ...
- 2018值得选用的五个Linux服务器发行版
[IT168 编译]据最新统计,目前Linux发行版约有300种,几乎都可以作为服务器系统运行.在Linux早期,几乎所有发行版都是“万能”发行版,专门的Linux服务器发行版并不火热,到21世纪初, ...
- Storm并行度详解
一.Storm并行度相关的概念 Storm集群有很多节点,按照类型分为nimbus(主节点).supervisor(从节点),在conf/storm.yaml中配置了一个supervisor,有多个槽 ...
- Flutter实战视频-移动电商-12.首页_GridView类别导航制作
12.首页_GridView类别导航制作 首页导航区的制作 外面用一个gridview来写.里面单独提出来 新建导航组件 还是在home_page.dart里面写代码 新建一个静态的组件: 快捷键写组 ...
- Apache CXF简介
Apache CXF是一个开源的,全功能的,容易使用的Web服务框架.CXF是由Celtix和XFire合并,在Apache软件基金会共同完成的.CXF的名字来源于"Celtix" ...
- Ubuntu 下编译libjingle-0.6.14 (转载)
转自:http://blog.csdn.net/feifei454498130/article/details/8197103 添加依赖库: sudo apt-get install build-es ...