頭文件 p167
#include <boost/format.hpp> using namespace boost;
一個簡單的例子
#include <boost/format.hpp> using namespace boost; int main() { cout<< format("%s:%d+%d=%d\n")%"sum"%1%2%(1+2); format fmt("(%1% + %2%) * %2% = %3%\n"); fmt%2%5; fmt%((2+5)*5); cout<<fmt.str(); return 0; }
%X%指示參數的位置,類似C#語言
高級用法 P189
basic_format& bind_arg(int argN, const T& val)
把格式化字符串第argN位置的輸入參數固定爲val, 即使調用clear()也保持不變,除非調用clear_bind()或clear_binds()。
baisc_format& clear_binds()
取消格式化字符串所有位置的參數綁定,並調用clear().
basic_format& modify_item(int itemN, T manipulator)
設置格式化字符串第itemN位置的格式化選項,manipulator是一個
boost::io::group()
返回的對象。boost::io::group(T1 a1, ..., Var const& var)
它是一個模板函數,最多支持10個參數(10個重載形式),可以設置IO流操縱器指定格式或輸入參數值,IO流操縱器位於頭文件
<iomanip>
.#include <boost/format.hpp> #include <iomanip> using namespace boost; using boost::io::group; int main() { //聲明format對象,有三個輸入參數,五個格式化選項 format fmt("%1% %2% %3% %2% %1% \n"); cout << fmt %1 %2 %3; fmt.bind_arg(2,10); //將第二個輸入參數固定爲數字10 cout << fmt %1 %3; fmt.clear(); //清空緩衝,但綁定的參數不變 //在%操作符中使用group(),指定IO流操縱符第一個參數顯示爲八進制 cout << fmt % group(showbase,oct, 111) % 333; fmt.clear_binds(); //清除所有綁定參數 //設置第一個格式化項,十六進制,寬度爲8, 右對齊, 不足位用*填充 fmt.modify_item(1, group(hex, right, showbase, setw(8), setfill('*'))); cout << fmt % 49 % 20 % 100; }
boost::format常用用法
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.