参考原著地址:http://einverne.github.io/post/2015/12/boost-learning-note-1.html

转换对象要求

lexical_cast 对转换对象有如下要求:

  1. 转换起点对象是可流输出的,即定义了 operator«
  2. 转换终点对象是可流输入的,即定义了 operator»
  3. 转换终点对象必须是可缺省构造和可拷贝构造的

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的更多相关文章

  1. BOOST学习笔记

    BOOST学习笔记 1 tool #pragma once #include <vector> #include "boost/noncopyable.hpp" #in ...

  2. boost 学习笔记 2: timer

    boost 学习笔记 2: timer copy from:http://einverne.github.io/post/2015/12/boost-learning-note-2.html 1:ti ...

  3. boost 学习笔记 0: 安装环境

    boost 学习笔记 0: 安装环境 最完整的教程 http://einverne.github.io/post/2015/12/boost-learning-note-0.html Linux 自动 ...

  4. boost学习笔记(七)---date_time库

    date_time库的日期基于格里高利历,支持从1400-01-01到9999-12-31之间的日期计算 #define BOOST_DATE_TIME_SOURCE #include <boo ...

  5. Boost学习笔记(六) progress_display注意事项

    progress_display可以用作基本的进度显示,但它有个固有的缺陷:无法把进度显示输出与程序的输出分离. 这是因为progress_display和所有C++程序一样,都向标准输出(cout) ...

  6. Boost学习笔记(五) progress_display

    progress_display可以在控制台显示程序的执行进度,如果程序执行很耗费时间,那么它能够提供一个友好的用户界面 #include <boost\timer.hpp> #inclu ...

  7. Boost学习笔记(三) progress_timer

    progress_timer也是一个计时器,它继承自timer,会在析构时自动输出时间,省去timer手动调用elapsed()的工作,是一个用于自动计时相当方便的小工具. #include < ...

  8. Boost学习笔记(二) 时间与日期

    timer库概述 timer库包含三个组件:分别是计时器类timer.progress_timer和进度指示类progress_display timer 主要作用是计时,精确度是毫秒级.下面是一个简 ...

  9. Boost学习笔记(一) 什么是Boost

    Boost库是一个功能强大.构造精巧,跨平台.开源并且完全免费的C++程序库

随机推荐

  1. DevExpress控件使用方法:第二篇 barManager

    标题栏 一.Bars 1.   把BarManager组件添加到窗体中后,会自动创建三个空的 bars: 主菜单(通常位于窗体顶部).顶部工具栏.窗体底部的状态栏. 2.   隐藏左侧的竖线和右边的箭 ...

  2. VIM命令操作

    退出命令 :wq 保存并退出 ZZ 保存并退出 :q! 强制退出并忽略所有更改 :e! 放弃所有修改,并打开原来文件.

  3. get_client_ip() 获取IP地址

    get_client_ip()获取ip地址,在开启IPv6协议的主机上会全部返回0.0.0.0原因是他会把ipv6地址认为是非法地址而转换成0.0.0.0,而ipv4地址在ipv6主机上用get_cl ...

  4. pycharm加载多个项目

    菜单位置:File -> Settings -> Project:xxx -> Project Stucture Project:xxx中xxx一般是已有项目的名称 窗口右侧上点击A ...

  5. 记录一次linux删除mysql

    service mysqld status service mysqld stop ps –ef | grep mysql perl-DBD-MySQL-4.013-3.el6.x86_64 //删除 ...

  6. tob toc tovc什么意思

    先说一下TOB.TOC.TOVC的含义.B:business (企业)C:customer(消费者)VC:Venture Capital(风险投资) to b产品是根据公司战略或工作需要,构建生态体系 ...

  7. WEKA从sqlite数据库文件导入数据

    1.编写代码的方式 只需要在java工程中导入weka.jar和sqlite-jdbc-3.8.7.jar两个jar包, weka.jar可以在weka的安装路径下找到, sqlite-jdbc-3. ...

  8. 安装Microsoft SQL server Management Studio Express 2005 错误码是29506解决方案

    安装Microsoft SQL server Management Studio Express 2005,安装程序在安装此软件包时遇到一个错误,这可能表示此软件包有错.错误码是29506”权限问题. ...

  9. pytest.10.使用fixture参数化测试预期结果

    From: http://www.testclass.net/pytest/test_api_with_expected_result/ 背景 接上一节v2ex网站的查看论坛节点信息的api. 我们在 ...

  10. 关于dubbo通信协议之对比

    对dubbo的协议的学习,可以知道目前主流RPC通信大概是什么情况, dubbo共支持如下几种通信协议: dubbo:// rmi:// hessian:// http:// webservice:/ ...