boost 学习笔记 1: lexical_cast
参考原著地址:http://einverne.github.io/post/2015/12/boost-learning-note-1.html
转换对象要求
lexical_cast 对转换对象有如下要求:
- 转换起点对象是可流输出的,即定义了 operator«
- 转换终点对象是可流输入的,即定义了 operator»
- 转换终点对象必须是可缺省构造和可拷贝构造的
C++中内建类型(int,double等)和std::string 都是符合三个条件的。
#include <boost/lexical_cast.hpp>
using namespace boost;
#include <iostream>
using namespace std;
using boost::lexical_cast; int main()
{
//lexical_cast在转换字符串时,字符串中只能包含数字和小数点,不能出现除e/E以外的英文字符或者其他非数字字符。
int a = lexical_cast<int>("");
long b = lexical_cast<long>("");
double c = lexical_cast<double>("123.12"); cout<< a << b << c <<endl; //数字转换成字符串时不支持高级格式控制
string str = lexical_cast<string>();
cout<<str<<endl; cout<<lexical_cast<string>(1.23)<<endl;
cout<<lexical_cast<string>(0x11)<<endl; //try catch
//当 lexical_cast 无法执行转换时会抛出异常 bad_lexical_cast ,它是 std::bad_cast 的派生类。可以使用 try/catch 块来保护代码
try
{
cout<<lexical_cast<int>("0x100");
cout<<lexical_cast<bool>("false");
}
catch(bad_lexical_cast& e)
{
cout<<"error:"<<e.what()<<endl;
}
char i = getchar();
return ;
}
boost 学习笔记 1: lexical_cast的更多相关文章
- BOOST学习笔记
BOOST学习笔记 1 tool #pragma once #include <vector> #include "boost/noncopyable.hpp" #in ...
- boost 学习笔记 2: timer
boost 学习笔记 2: timer copy from:http://einverne.github.io/post/2015/12/boost-learning-note-2.html 1:ti ...
- boost 学习笔记 0: 安装环境
boost 学习笔记 0: 安装环境 最完整的教程 http://einverne.github.io/post/2015/12/boost-learning-note-0.html Linux 自动 ...
- boost学习笔记(七)---date_time库
date_time库的日期基于格里高利历,支持从1400-01-01到9999-12-31之间的日期计算 #define BOOST_DATE_TIME_SOURCE #include <boo ...
- Boost学习笔记(六) progress_display注意事项
progress_display可以用作基本的进度显示,但它有个固有的缺陷:无法把进度显示输出与程序的输出分离. 这是因为progress_display和所有C++程序一样,都向标准输出(cout) ...
- Boost学习笔记(五) progress_display
progress_display可以在控制台显示程序的执行进度,如果程序执行很耗费时间,那么它能够提供一个友好的用户界面 #include <boost\timer.hpp> #inclu ...
- Boost学习笔记(三) progress_timer
progress_timer也是一个计时器,它继承自timer,会在析构时自动输出时间,省去timer手动调用elapsed()的工作,是一个用于自动计时相当方便的小工具. #include < ...
- Boost学习笔记(二) 时间与日期
timer库概述 timer库包含三个组件:分别是计时器类timer.progress_timer和进度指示类progress_display timer 主要作用是计时,精确度是毫秒级.下面是一个简 ...
- Boost学习笔记(一) 什么是Boost
Boost库是一个功能强大.构造精巧,跨平台.开源并且完全免费的C++程序库
随机推荐
- SWF 文件不能本地访问 只有仅限于文件系统的 SWF 文件
http://blog.163.com/vituk93@126/blog/static/1709580342012512112757505/ SWF 文件不能被本地访问 不能访问本地 只有仅限于文件系 ...
- Bjarne Stroustrup announces C++ Core Guidelines
This morning in his opening keynote at CppCon, Bjarne Stroustrup announced the C++ Core Guidelines ( ...
- react 学习资料
react 学习资料 项目 学习资料 react 中文版:https://doc.react-china.org/ react-router https://reacttraining.com/rea ...
- https ssl 请求过程详解
http 协议:http 协议是一种无状态,短链接的 通信协议,http 协议建立在 tcp 协议之上. http 协议 分成 三个 部分 请求行,请求头,请求体 请求行: 就是访问的地址 ( 包含 ...
- PyCharm 设置Python 文件头部模板
1. 菜单栏-> File -> Settings -> Editor -> File and Code Templates ->Python Script 2.配置头文 ...
- 配置中心Server端
为什么需要统一配置中心 1.不方便维护.一个功能被多个人开发,如果其中一个人修改了配置文件,另外一个人测试之前的功能,准备使用之前的配置. 2.配置内容安全与权限.线上的配置是不会对开发公开,特别是数 ...
- Visual Studio 2008(C#)XML注释提取成帮助文档的方法
Visual Studio 2008(C#)XML注释提取成帮助文档的方法 1.给方法和类添加XML注释 可以手动添加,具体规则可以看MSDN:http://msdn.microsoft.co ...
- Excel技巧--时尚的圆环比例图
如上图,制作方法如下: 1.创建圆环图:选择表格,点击“插入”-->点击 圆环图. 2.删除图中的标题和标记,将圆环内径缩至最小: 3.复制表格的数据,重复两次粘贴到图表中: 4.依次选择内两环 ...
- Atom窗口切换和放大或者缩小
Atom窗口切换和放大或者缩小 快捷键就是 F11
- String[]字符串数组,按字典顺序排列大小
package ltb6w1; public class WordSort1 { private String[] a= {"hello","world",&q ...