头文件 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常用用法
最新推荐文章于 2023-08-22 21:43:07 发布