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++程序库
随机推荐
- http笔记汇总
网上笔记参考: https://juejin.im/post/5b34e6ba51882574d20bbdd4#heading-8 http://dy.163.com/v2/article/detai ...
- Autofac学习之三种生命周期:InstancePerLifetimeScope、SingleInstance、InstancePerDependency 【转载】
InstancePerLifetimeScope:同一个Lifetime生成的对象是同一个实例 SingleInstance:单例模式,每次调用,都会使用同一个实例化的对象:每次都用同一个对象: In ...
- openstack--4--控制节点安装配置glance
Glance相关介绍 image Service 的功能是管理 Image,让用户能够发现.获取和保存 Image.在 OpenStack 中,提供 Image Service 的是 Glance,其 ...
- leftJoin鏈錶查詢
//待使用券碼 $code_record_no = DB::table('fook_platform_order as a') ->select('o.code','o.apportion_bi ...
- virtualbox下centos虚拟机安装增强工具教程和常见错误解决
VirtualBox 4.3.6上安装CentOS 6.5 https://my.oschina.net/tashi/blog/190060 错误1.Building the main Guest A ...
- 【Spark Java API】broadcast、accumulator
转载自:http://www.jianshu.com/p/082ef79c63c1 broadcast 官方文档描述: Broadcast a read-only variable to the cl ...
- 关于IP核中中断信号的使用---以zynq系统为例
关于IP核中中断信号的使用---以zynq系统为例 1.使能设备的中断输出信号 2.使能处理器的中断接收信号 3.连接IP核到处理器之间的中断 此处只是硬件的搭建,软件系统的编写需要进一步研究. 搭建 ...
- 实现类QQ的编辑框
第一步,平面效果.Windows系统有几个消息专门用来处理Windows组件的边框部位,那就是WM_NCCALCSIZE和WM_NCPAINT这两个消息,从消息名字看来NC这个就代表着No Clien ...
- sql 语句收集
SELECT type as 'plan cache store', buckets_count FROM sys.dm_os_memory_cache_hash_tables WHERE type ...
- 黄聪:如何使用钩子定制WordPress添加媒体界面,去除不需要的元素
原文:http://www.solagirl.net/customize-wordpress-media-upload-ui.html WordPress编写文章界面的添加媒体按钮允许用户上传多媒体文 ...