格式化輸出format
零、前言
格式化輸出是每一個程序員經常使用到的功能,把一系列參數格式化成一個字符串,在工作中中會經常用到,C語言提供了printf這個格式化輸出函數,它因簡單高效而被廣泛應用,對其他語言影響深遠,但是它的一個缺點就是不會做類型安全檢查。boost庫提供了一個類似printf功能的組件boost.format,它不僅實現了printf的功能,還增加了類型安全檢查和一些高級用法。
一、boost.format組件
1、boost.format組件介紹
C語言中的printf函數使用了可變參數(瞭解可變參使用點擊)來實現被格式化的數據,但它是不安全的。boost.format組件爲了增加安全性,模仿了流操作符<<,重載了二元操作符operator%作爲參數輸入符來實現可變參數的功能。
format組件的構造函數接受string類型作爲格式化字符串,並提供了成員函數str()來返回已經格式化好的字符串,如果沒有得到按照規則的格式化數據則會拋出異常。
瞭解可變參使用點擊:C語言可變參
2、boost.format格式化語法
format使用了類似printf的格式化語法規則,比如說對齊、寬度、精度、字符類型等等。除了以上這些經典的printf用法,還增加了%|spec|來區分格式化選項與普通字符,%N%來標記第N個參數的佔位等功能,在語法上比printf的更加完善和易懂。
3、代碼示例
// BoostFormat.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
//
#include <iostream>
#include <boost/format.hpp>
using namespace std;
using namespace boost;
int main()
{
cout << "----------------boost.format的使用示例-----------------" << endl;
cout << "-------------------------------" << endl;
boost::format fmt("(%1% + %2%) * %3% = %4%\n");
fmt % ((2 + 3) * 4);
cout << fmt.str();
cout << "-------------------------------" << endl;
cout << boost::format("%s: %d+%d=%d\n") % "sum" % 5 % 6 % (5 + 6);
cout << "-------------------------------" << endl;
//對下面的表達式進行解釋,||用來區分格式化選項功能,可以不要,當表達式中含有空格等特殊字符佔位時,使用||表達更容易區分些
// %|04d|輸出寬度爲4的數值,不足位用0補充
// %|-8.4f|左對齊,輸出寬度爲8,小數位保留4爲的浮點數
// %| 8s|輸出寬度爲8的字符串,不足位用空格填補的字符串
// %|06X|輸出寬度爲6的大寫16進制整數,不足位用0填補
boost::format tmpFormat("%|04d|\n %|-8.4f|\n %| 8s|\n %|06X|\n");
tmpFormat % 44 % 3.1415926 %"123456" % 48;
// 使用boost自帶的str()函數獲取表達式,也可以使用format的自帶成員函數str(),注意二者的使用不同
string tmpFormatStr = boost::str(tmpFormat);
cout << tmpFormatStr << endl;
std::cout << "Hello World!\n";
getchar();
}
運行結果:
二、boost.format組件的高級用法
1、format的高級用法
format提供的一些函數可以在程序運行時修改格式化選項和綁定輸入參數功能。一些函數介紹如下:
bind_arg(int argn, const T &value):把第argn位的輸入參數綁定爲一個固定的數值value。
clear_bind(int argn):取消第argn位的參數綁定。
clear_binds():取消所有位的參數綁定。
modify_item(int itemn, T manipulator):設置格式化字符串itemn的格式選項,manipulator是一個boost::io::group()返回的對象。
boost::io::group(T1 a1,…,Var const & var):這是一個模板函數,可以設置輸出或者輸入流控制器來指定格式或者輸入參數值。
2、代碼示例
// BoostFormat.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
//
#include <iostream>
#include <boost/format.hpp>
#include <iomanip>
using namespace std;
using namespace boost;
int main()
{
cout << "----------------boost.format的高級用法-----------------" << endl;
// 5個格式化選項,3個輸入參數
boost::format fmt("%1% %2% %3% %1% %2%");
fmt % 2 % 4 % 6;
cout << fmt.str() << endl;
cout << "------------------------------------------" << endl;
// 綁定第二個參數爲8
fmt.bind_arg(2, 8);
fmt % 7 % 9;
cout << fmt.str() << endl;
cout << "------------------------------------------" << endl;
// 清空fmt的緩存
fmt.clear();
// 把第一個輸入參數設置爲8進制的數
fmt % boost::io::group(showbase,oct,12) % 33;
cout << fmt.str() << endl;
cout << "------------------------------------------" << endl;
// 清楚所有的綁定
fmt.clear_binds();
// 設置第一個格式化項爲十六進制,寬度爲8,右對齊,不足位用$補充
fmt.modify_item(1, boost::io::group(hex, right, showbase,setw(8),setfill('$')));
fmt % 48 % 20 % 100;
cout << fmt.str() << endl;
std::cout << "Hello World!\n";
getchar();
}
運行結果:
原創不易,點贊鼓勵一下吧!