boost.lexical_cast 学习
1,字符串 到 数值类型的转换
2,数值 到 字符串的转换
3,异常处理情况
4,boost::lexical_cast 的原型:
template<typename Target, typename Source>
Target lexical_cast(Source arg);
lexical_cast 是依赖于字符串流 std::stringstream 的,其原理也是相当的简单:把源类型 (Source) 读入到字符流中,再写到目标类型 (Target) 中。但这里同时也带来了一些限制:
- 输入数据 (arg) 必须能够 “完整” 地转换,否则就会抛出 bad_lexical_cast 异常。例如:
int i = boost::lexical_cast<int>("123.456"); // this will throw
因为 “123.456” 只能 “部分” 地转换为 123,不能 “完整” 地转换为 123.456,还是让我们需要适当的注意一些这两个模板参数就好了。
- 由于 Visual C++ 6 的本地化(locale)部分实现有问题,如果使用了非默认的 locale,可能会莫名其妙地抛出异常。
- 源类型 (Source) 必须是一个可以输出到输出流的类型(OutputStreamable),也意味着该类型需要 operator<< 被定义。
- 同样的,目标类型 (Target) 必须是一个可以输入到输入流的类型 (InputStreamable),也意味着该类型需要 operator>> 被定义。
- 另外,Both Source and Target are CopyConstructible。
- Target is DefaultConstructible。
其中,下面的四个限制是在使用自定义类型之间转换时必须做的一些工作的(当然流行的使用的 C 库函数等等都是不可以处理自定义类型之间的转换的)。
- #include <boost/lexical_cast.hpp>
- #include <iostream>
- #include <string>
- #define ERROR_LEXICAL_CAST 1
- int main()
- {
- using boost::lexical_cast;
- int a = 0;
- double b = 0.0;
- std::string s = "";
- int e = 0;
- try
- {
- // ----- 字符串 --> 数值
- a = lexical_cast<int>("123");
- b = lexical_cast<double>("123.12");
- // ----- 数值 --> 字符串
- s = lexical_cast<std::string>("123456.7");
- // ----- 异常处理演示
- e = lexical_cast<int>("abc");
- }
- catch(boost::bad_lexical_cast& e)
- {
- // bad lexical cast: source type value could not be interpreted as target
- std::cout << e.what() << std::endl;
- return ERROR_LEXICAL_CAST;
- }
- std::cout << a << std::endl; // 输出:123
- std::cout << b << std::endl; // 输出:123.12
- std::cout << s << std::endl; // 输出:123456.7
- return 0;
- }
boost.lexical_cast 学习的更多相关文章
- boost asio 学习(九) boost::asio 网络封装
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=10 9. A ...
- boost asio 学习(八) 网络基础 二进制写发送和接收
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=9 8. Net ...
- boost asio 学习(七) 网络基础 连接器和接收器(TCP示例)
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=8 7. Net ...
- boost库学习之开篇
本系列文章使用boost_1.58.0版本. 一.欢迎使用boost C++库 boost致力于提供一个免费的.便携的源代码级的库. 我们重视那些与C++标准一起工作良好的库.boost库将要成为一个 ...
- boost/lexical_cast.hpp的简单使用方法_行动_新浪博客
boost/lexical_cast.hpp的简单使用方法_行动_新浪博客 boost/lexical_cast.hpp的简单使用方法 (2010-03-19 16:31:13) ...
- boost asio 学习(一)io_service的基础
原文 http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio/ 编译环境 b ...
- boost.asio学习-----reslover 域名解析
将域名解析为ip地址并输出: #include "stdafx.h" #include "boost/asio.hpp" #include <boost/ ...
- 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 ...
随机推荐
- XenServer中虚拟机和快照导出与导入
我们在工作中经常会遇到,把Xenserver中的虚拟机或者快照导出,然后导入到另一台Xenserver,或者导出来备份下来,以防虚拟机出现故障. 下面介绍一下用xe命令如何导出/导入虚拟机或快照,当然 ...
- C++回顾day02---<引用>---待补充
一:引用概念---引用就是为一个变量起一个别名 每个变量都是指向一块内存空间的标识,引用就是重新设置一个标识,但是这个标识还是指向同一个内存空间 和指针类似(其实引用本质就是使用了一个常指针 cons ...
- JAVA核心技术I---JAVA基础知识(映射Map)
一:映射Map分类 二:Hashtable(同步,慢,数据量小) –K-V对,K和V都不允许为null –同步,多线程安全 –无序的 –适合小数据量 –主要方法:clear, contains/con ...
- Storm安装部署
1.从官网下载安装包,并通过Xftp5上传到机器集群上 下载apache-storm-1.2.1.tar.gz 版本,并通过Xftp5上传到hadoop机器集群的第一个节点node1上的/opt/up ...
- git步骤
1.New一个Repositories 2.拿到这个仓库的URL 3.git clone https://github.com/zhuobo/new.git 4.进入到clone下来的文件夹,然后gi ...
- HanLP中人名识别分析
HanLP中人名识别分析 在看源码之前,先看几遍论文<基于角色标注的中国人名自动识别研究> 关于命名识别的一些问题,可参考下列一些issue: 名字识别的问题 #387 机构名识别错误 关 ...
- 关闭Android ActionBar
修改Styles.xml中 <resources> <!-- Base application theme. --> <style name="AppTheme ...
- 设置 Visual Studio IIS Express 站点局域网访问
Ø Visual Stuido 的 IIS Express运行一个网站时,默认地址是这样的:http://localhost:23167/Cache/Three,其中 localhost 表示本机, ...
- PHP+MySql+Bootstrap实现用户界面数据的删除、修改与批量选择删除——实例操作
第一步:在数据库中建立要操作的信息表 如下图: 第二步:实现对该信息表中数据的删除功能 代码如下:main(主页面) <!DOCTYPE html><html> < ...
- matplotlib-区域填充
import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl import datetime #解决能显示中文 ...