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++程序库
随机推荐
- 在单文件组件中,引入安装模块里的css的2种方式:script中引入、style中引入
在单文件组件中,引入安装模块里的css的2种方式:script中引入.style中引入 1.script中引入 <script> import 'bulma/css/bulma.css' ...
- checkbox复选框,如何让其勾选时触发一个事件,取消勾选时不触发
<input type="checkbox" onclick="checkboxOnclick(this)" /> <script> f ...
- C++Builder XE7 中“匿名”方法实现
class TMyProc : public TCppInterfacedObject<TThreadProcedure> { private: String p1; String p2; ...
- 如何在linux服务器上使用hanlp
关于如何在linux服务器上使用hanlp也有分享过一篇,但分享的内容与湘笑的这篇还是不同的.此处分享一下湘笑的这篇hanlp在linux服务器上使用的文章,供新手朋友学习之用. 本文主要工作是在li ...
- Java生成PDF文档(表格、列表、添加图片等)
需要的两个包及下载地址: (1)iText.jar:http://download.csdn.net/source/296416 (2)iTextAsian.jar(用来进行中文的转换):http:/ ...
- postfix配置spf认证和dkim认证
1.为邮箱域名添加spf认证: 登录域名解析控制台添加txt记录: v=spf1 include:spf1.domain.com ~all spf1.domain.com A记录解析到你的固定IP ...
- mac里安装Mycrypt扩展
https://jingyan.baidu.com/article/e3c78d644cf1ed3c4c85f5a8.html 先用homebrew安装mycrpt 再下载php5.6版本源码 然后进 ...
- 魔豆love移植
其中love.sh代码如下: #!/bin/sh if [ ! -f "$app_conf" ]; then echo url=http://modou.ydjiao.com/ap ...
- 进程池pool
如果有多个进程,同一时间只能有限个给cpu运行 from multiprocessing import Process,Pool import time,os def bar(arg): print( ...
- Restful API设计规范及实战【说的比较清楚了】
Restful API设计规范及实战 Restful API的概念在此就不费口舌了,博友们网上查哈定义文章很多,直入正题吧: 首先抛出一个问题:判断id为 用户下,名称为 使命召唤14(COD14 ...