浅尝boost之format

概述
     std::string是个很不错的东东,但实际使用时基本在每个程序里都会遇到不愉快的事情:格式化字符串。我甚至由于这个原因在代码里引入平台有关的MFC,ATL等本来不需要在项目中使用的一些重量级的框架,就为了能轻松的做格式化字符串
:-)
。曾尝试过将ATL::CString的format函数提取出来使用,但ATL::CString的底层调用了windows独有函数,无法跨越平台。当然,现在有了boost::format,我们不用再担心了。boost::format重载了'%'操作符,通过多次调用'%'操作符就能将参数非常方便格式化成字符串,并实现了ATL::CString和C#中的string两者的格式化字符串功能。除了语法刚开始感觉到怪异,功能足以让人感觉到兴奋!

一、boost::format工作的方式
  基本的语法,boost::format( format-string ) % arg1 % arg2 % ... % argN
 
 下面的例子说明boost::format简单的工作方式

// 方式一
cout << boost::format("%s") % "输出内容" << endl;

// 方式二
std::string s;
s = str( boost::format("%s") % "输出内容" );
cout << s << endl;

// 方式三
boost::format formater("%s");
formater % "输出内容";
std::string s = formater.str();
cout << s << endl;

// 方式四
cout << boost::format("%1%") % boost::io::group(hex, showbase, 40) << endl;

二、boost::format实际使用的实例
 
 格式化语法: [ N$ ] [ flags ] [ width ] [ . precision ] type-char

// ATL::CString风格
cout << boost::format("\n\n%s"
"%1t 十进制 = [%d]\n"
"%1t 格式化的十进制 = [%5d]\n"
"%1t 格式化十进制,前补'0' = [%05d]\n"
"%1t 十六进制 = [%x]\n"
"%1t 八进制 = [%o]\n"
"%1t 浮点 = [%f]\n"
"%1t 格式化的浮点 = [%3.3f]\n"
"%1t 科学计数 = [%e]\n"
) % "example :\n" % 15 % 15 % 15 % 15 % 15 % 15.01 % 15.01 % 15.01 << endl;

// C#::string风格
cout << boost::format("%1%"
"%1t 十进制 = [%2$d]\n"
"%1t 格式化的十进制 = [%2$5d]\n"
"%1t 格式化十进制,前补'0' = [%2$05d]\n"
"%1t 十六进制 = [%2$x]\n"
"%1t 八进制 = [%2$o]\n"
"%1t 浮点 = [%3$f]\n"
"%1t 格式化的浮点 = [%3$3.3f]\n"
"%1t 科学计数 = [%3$e]\n"
) % "example :\n" % 15 % 15.01 << endl;


输出结果
/*
example :
十进制 = [15]
格式化的十进制 = [ 15]
格式化十进制,前补'0' = [00015]
十六进制 = [f]
八进制 = [17]
浮点 = [15.010000]
格式化的浮点 = [15.010]
科学计数 = [1.501000e+001]
*/

三、boost::format新的格式说明符
 
 %{nt}
 当n是正数时,插入n个绝对制表符
 cout << boost::format("[%10t]")  << endl;
 
 %{nTX}
 使用X做为填充字符代替当前流的填充字符(一般缺省是一个空格)
 cout << boost::format("[%10T*]")  << endl;

四、异常处理

一般写法:

try
{
cout << boost::format("%d%d") % 1 << endl;
}
catch(std::exception const & e)
{
cout << e.what() << endl;

// 输出内容:
// boost::too_few_args: format-string refered to more arguments than were passed
}

boost::format的文档中有选择处理异常的办法,不过个人感觉实用性可能不强,下面是文档中的例子

// boost::io::all_error_bits selects all errors
// boost::io::too_many_args_bit selects errors due to passing too many arguments.
// boost::io::too_few_args_bit selects errors due to asking for the srting result before all arguments are passed

boost::format my_fmt(const std::string & f_string)
{
using namespace boost::io;
format fmter(f_string);
fmter.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
return fmter;
}
cout << my_fmt(" %1% %2% \n") % 1 % 2 % 3 % 4 % 5;

五、还有其它一些功能,但暂时感觉派不上用处,就不去深究了。

Boost format的更多相关文章

  1. boost::format(字符串格式化库)

    这段时间学习boost库的使用,撰文一方面留以备用,另一方面就是shared精神. format主要是用来格式化std::string字符串以及配合std::cout代替C语言printf() 使用f ...

  2. 【原创】请不要对Boost Format使用Byte作为参数

    曾几何时我们可以肆无忌惮的对sprintf传入BYTE等类型作为参数,只要你指定的为%D即可打印出对应的数字 但是boost format不可以,当你发生类型截断,错误,异常,请尽快查看你传入的类型是 ...

  3. 一起学习Boost标准库--Boost.texical_cast&format库

    今天接续介绍有关字符串表示相关的两个boost库: lexical_cast 将数值转换成字符串 format 字符串输出格式化 首先,介绍下lexical_cast ,闻其名,知其意.类似C中的at ...

  4. boost.log要点笔记

    span.kw { color: #007020; font-weight: bold; } code > span.dt { color: #902000; } code > span. ...

  5. boost asio 异步实现tcp通讯

    ---恢复内容开始--- asioboost   目录(?)[-] 一前言 二实现思路 通讯包数据结构 连接对象 连接管理器 服务器端的实现 对象串行化   一.前言 boost asio可算是一个简 ...

  6. boost::coroutine 无法显示调用栈

    boost::coroutine 无法显示调用栈(金庆的专栏)一例因 boost::format() 格式化参数个数错误造成的 coredump,因为使用了 boost::coroutine, 无法显 ...

  7. boost学习目录

    Boost之数值转换lexical_cast https://www.cnblogs.com/TianFang/archive/2013/02/05/2892506.html Boost之字符串算法s ...

  8. boost常用库案例

    1.boost::any boost::any是一种通用的数据类型,可以将各种类型包装后统一放入容器内,最重要的它是类型安全的.有点象COM里面的variant. 使用方法: any::type()  ...

  9. boost-使用format和lexical_cast实现数字和字符串之间的转换

    使用boost的format可以实现数字到string的格式化转换,boost的lexical_cast可以实现string到数值的转换,eg: #include "boost/format ...

随机推荐

  1. 文件和目录之chown、fchown和lchown函数

    下面几个chown函数可用于更改文件的用户ID和组ID. #include <unistd.h> int chown( const char *pathname, uid_t owner, ...

  2. MySQL之事务隔离级别--转载

    转自:http://793404905.blog.51cto.com/6179428/1615550 本文通过实例展示MySQL事务的四种隔离级别. 1 概念阐述 1)Read Uncommitted ...

  3. 谈谈MVVM和链式网络请求架构

    前言 前一段时间一直在学习iOS的架构.为什么呢? 公司的架构一直是MVC,当我们正式上线的时候,项目已经有了超十万行代码.主要的VC一般都有2000行代码以上. 关键是,目前版本我们只做了三分之一的 ...

  4. 微信公众平台接口API

    <?php /** * Author: helen * CreateTime: 2015/12/9 20:14 * description: 微信公众平台接口API */ class Wecha ...

  5. [转]Oracle开发专题之:%TYPE 和 %ROWTYPE

    本文转自:http://www.cnblogs.com/kingjiong/archive/2009/02/19/1393837.html 1. 使用%TYPE 在许多情况下,PL/SQL变量可以用来 ...

  6. CountDownLatch(闭锁)

    一.闭锁(Latch)    闭锁(Latch):一种同步方法,可以延迟线程的进度直到线程到达某个终点状态.通俗的讲就是,一个闭锁相当于一扇大门,在大门打开之前所有线程都被阻断,一旦大门打开所有线程都 ...

  7. Ubuntu lnmp安装记录

    参考了: Ubuntu 14.04 LTS 安装 LNMP Nginx\PHP5 (PHP-FPM)\MySQL 在访问 info.php的时候,我显示的是502错误.后面我参考了: Ubuntu15 ...

  8. Windows之vmware安装破解版错误汇总

    A.错误: units specified don't exist, SHSUCDX can't install A.解决: 虚拟机配置->CD/DVD->IDE(0,0) B:错误: n ...

  9. JAXB - Annotations, Annotations for Enums: XmlEnum, XmlEnumValue

    An enum type is annotated with XmlEnum. It has an optional element value of type java.lang.Class whi ...

  10. 20160509-hibernate--继承映射

    继承映射 对象模型(Java类结构) 一个类继承体系一张表(subclass)(表结构) 1.一个类继承体系一张表(subclass)(映射文件) <class name="Emplo ...