[转] 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 ...
随机推荐
- ansible j2文件注意事项
j2文件中使用变量的时候,不需要 {{}} 遇到if,for等条件判断语句,变量的话,只需要变量名即可, 字符串需要 加上引号.
- RTree算法Java实现 JSI RTree Library的调用实例 标签:jsi-rtree-library
1. [代码]jsi-rtree-library /** * */package com.mycompany.project; //package net.sourceforge.jsi.examp ...
- js 分享代码--完整示例代码
<div class="bdsharebuttonbox" data-tag="share_1"> <a class="bds_ms ...
- Derived 派生类
#include "stdafx.h"#include "iostream" using namespace std; class Base1{public: ...
- 如何使用Psyco为你的Python程序提速
psyco加速Python执行速度的方法:要求: 版本对照:File name Python versions Well-tested withpsyco-x.y-win32-py ...
- bzoj 3809 Gty的二逼妹子序列——莫队+分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 容易想到树状数组维护值域.但修改和查询都是 log 太慢. 考虑有 nsqrt(n) ...
- linux sdk配置
http://blog.csdn.net/wudj810818/article/details/51251408,环境变量配置不要用 SDK环境变量: export PATH=$PATH:/opt/a ...
- 2.11-2.12 HBase的数据迁移常见方式
一.importtsv 把hdfs中数据抽取到HBase表中: 1.准备数据 ##student.tsv [root@hadoop-senior datas]# cat student.tsv 100 ...
- c++拷贝函数详解(转)
一. 什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很简单的,例如 int a = 100; int b = a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变 ...
- TypeScript完全解读(26课时)_3.TypeScript完全解读-Symbol
ts中symbol的支持是按照es6的标准来的,只要我们学会es6中的symbol,就可以直接在ts中使用了 创建symbol 在example文件夹下新建symbol.ts 然后在根目录的index ...