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++程序库
随机推荐
- DevExpress控件使用方法:第一篇 gridControl详解
GridControl (1)层次设计器 有五种视图模式,banded gridview多行表头,数据还是一行一组,最靠近数据的表头与数据一一对应:advanced banded gridview多行 ...
- 对某个区间操作(sort,stable_sort,parital_sort,parital_sort_copy,nth_element,is_sorted)
sort //版本一 template <class RandomAccessIterator> void sort(RandomAccessIterator first,RandomAc ...
- Python __slots__的使用
每个类在实例化时都会被分配一个dict,通过 实例.__dict__来访问,dict记录了实例的所有属性 如: class Man(object): pass man = Man()print(man ...
- js检测字符串的字节数
在js中字符串可以存放数字,字母或者汉字,但是又一个问题就是,数字和字母都是占一个字节,而一个汉字占2个字节.如果在一个字符串中既有字母又有汉字怎么判断字节数呢 第一种简单粗暴 var str = ' ...
- Docker容器里的进程为什么要前台运行?相同的问题:docker运行apache为什么带FOREGROUND参数?docker运行nginx为什么带`daemon off`参数?
<第一本Docker书>里面,讲到Docker容器启动web服务时,都指定了前台运行的参数. 例如apache: ENTRYPOINT [ "/usr/sbin/apache2& ...
- docker 学习资料
docker 学习资料 学习资料 网址 Docker 教程(菜鸟教程) http://www.runoob.com/docker/docker-tutorial.html
- PyCharm 链接MySQL 数据库
1.View -> Tool Windows-Database; 2.添加数据源: 3.配置数据库的驱动信息: 4.设置驱动文件,勾选“Use Provided driver MySQL Con ...
- Hanlp在ubuntu中的使用方法介绍
HanLP的一个很大的好处是离线开源工具包,换而言之,它不仅提供免费的代码免费下载,而且将辛苦收集的词典也对外公开啦,此诚乃一大无私之举.我在安装的时候,主要参照这份博客: blog.csdn.net ...
- Redis:高性能文件缓存key-value储存
1.前言 a.Redis是一个开源,先进的key-value(键/值对)存储,并且勇于构建高性能,可扩展的Web应用程序的完美解决方案 b.Redis和Memcached的对比 b.1 Redis数据 ...
- 动态设置所有string字段不分词
PUT http://192.168.1.12:9200/test { "settings": { "number_of_shards": 3, &qu ...