boost之format库,格式化输出

来源:互联网 发布:linux强制退出vi q! 编辑:程序博客网 时间:2024/06/06 13:00

示例代码:

#include <iostream>#include <string>using namespace std;#include <boost/format.hpp>using namespace boost;int main(int argc, char*argv[]){//顺序不必一致format fmter("%2% %1%");fmter % 100;fmter % 200;cout<<"fmter:"<<fmter<<endl;//可重用fmter % 11;fmter % 22;cout<<"fmter:"<<fmter<<endl;//可直接转为字符串string s = fmter.str();cout<<"s:"<<s.c_str()<<endl;string s2 = str(format("%2% %1% %2% %1%") % "World" % "Hello");cout<<"s2:"<<s2.c_str()<<endl;//可以使用printf指示符cout<<format("%3.1f, %d") % 3.2 % 8<<endl;//其他用法cout<<format("(x,y) = (%+5d, %+5d)") % -23 % 35<<endl;cout<<format("(x,y) = (%|+5|, %|+5|)") % -23 % 35<<endl;cout<<format("(x,y) = (%1$+5d, %2$+5d)") % -23 % 35<<endl;cout<<format("(x,y) = (%|1$+5|, %|2$+5|)") % -23 % 35<<endl;return 0;}

输出结果:


原创粉丝点击