Boost学习之格式化输出--format

boost::format类提供了类似C语言里'printf'功能的格式化输出能力,当然功能更强大。

所需头文件:

#include <boost/format.hpp>

示例代码:

  1. #include <iostream>
  2. #include <string>
  3. #include <boost/format.hpp>
  4.  
  5. using namespace std;
  6. int _tmain(int argc, _TCHAR* argv[])
  7. {
  8.     // 使用%序号%的方式给出指示符, 后面用%连接对应的数据。
  9.     cout << boost::format("writing %1%,  x=%2% : %3%-th try") % "toto" % 40.23 % 50 << endl;
  10.     // 输出:writing toto,  x=40.23 : 50-th try
  11.    
  12.     // 可以延迟使用,顺序不必一致
  13.     boost::format fmter("%2% %1%");
  14.     fmter % 36;
  15.     fmter % 77;
  16.     cout << fmter << endl;
  17.     // 输出:77 36
  18.    
  19.     // 可重用
  20.     fmter % 12;
  21.     fmter % 24;
  22.     cout << fmter << endl;
  23.     // 输出:24 12
  24.  
  25.     // 可直接转成字符串
  26.     std::string s = fmter.str();
  27.     std::string s2 = str( boost::format("%2% %1% %2% %1%")%"World"%"Hello");
  28.  
  29.     cout << s << endl << s2 << endl;
  30.     // 输出:
  31.     // 24 12
  32.     // Hello World Hello World
  33.  
  34.     // 可以使用printf指示符
  35.     cout << boost::format("%3.1f - %.2f%%") % 10.0 % 12.5  << endl;
  36.     // 输出:10.0 - 12.50%
  37.  
  38.     // printf指示符里使用N$指定使用第几个参数
  39.     cout << boost::format("%2$3.1f - %1$.2f%%") % 10.0 % 12.5  << endl;
  40.     // 输出:12.5 - 10.00%
  41.  
  42.     cin.get();
  43.     return 0;
  44. }
#include <iostream>
#include <string>
#include <boost/format.hpp>

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    // 使用%序号%的方式给出指示符, 后面用%连接对应的数据。
    cout << boost::format("writing %1%,  x=%2% : %3%-th try") % "toto" % 40.23 % 50 << endl;
    // 输出:writing toto,  x=40.23 : 50-th try
   
    // 可以延迟使用,顺序不必一致
    boost::format fmter("%2% %1%");
    fmter % 36;
    fmter % 77;
    cout << fmter << endl;
    // 输出:77 36
   
    // 可重用
    fmter % 12;
    fmter % 24;
    cout << fmter << endl;
    // 输出:24 12

    // 可直接转成字符串
    std::string s = fmter.str();
    std::string s2 = str( boost::format("%2% %1% %2% %1%")%"World"%"Hello");

    cout << s << endl << s2 << endl;
    // 输出:
    // 24 12
    // Hello World Hello World

    // 可以使用printf指示符
    cout << boost::format("%3.1f - %.2f%%") % 10.0 % 12.5  << endl;
    // 输出:10.0 - 12.50%

    // printf指示符里使用N$指定使用第几个参数
    cout << boost::format("%2$3.1f - %1$.2f%%") % 10.0 % 12.5  << endl;
    // 输出:12.5 - 10.00%

    cin.get();
    return 0;
}

boost::format里的指示符语法大致有三大类:

继承并强化自printf的格式化字符串
    形式为: [ N$ ] [ flags ] [ width ] [ . precision ] type-char
    N$可选,指定使用第N个参数(注意,要么所有指示符都加此参数,要么都不加)
    接下来的参数可以参数printf的指示符,只是format为其中的flags添加了'_'和'='标志,用于指出内部对齐和居中对齐。
设置打印规则,它是printf参数的一个补充,只是为了更直观点。
    形式为: %|spec|
    如:%|1$+5|表示显示第一个参数,显示正负号,宽度为5
简单的位置标记
    形式为: %N%
    简单地声明显示第N个参数,优点是比较直观而且不用指定类型。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值