基本用法

std::cout << boost::format("%1% - %2%\n") % 3 % 4;
// prints "3 - 4\n"
std::cout << boost::format("%2% %1% %2%\n") % 1 % boost::io::group(setfill('X'), hex, setw(4), 16 + 3);
// prints "XX13 1 XX13\n"

基本格式化语法

起(%) 格式 尾(C Style) 尾(||) 尾(%)
+     +     “%s”、”%d”
+   + +     “%3s”、”%05d”
+ + + +     “%1$3s”、”%2$05d”
+   +   +   ”%|+5|”
+ + +   +   ”%|1$+5|”
+ +       + “%1% %2%”

可能抛出的异常

  • boost::io::bad_format_string
  • boost::io::too_many_args
  • boost::io::too_few_args

参考资料

  1. Boost Format library.