boost::format提供了一个和C的 printf类似的格式化字串语法定义,同时也保有了C++的 ostraem的各项优势,对于要做格式化输出的C++程序开发人员来说,boost::format是个相当好用的函数库。使用format需包含头文件boost/format.hpp。
typedef basic_format<char> format;
示例
#pragma warning(disable:4996)
#include<iostream>
using namespace std;
//包含头文件
#include<boost/format.hpp>
using boost::format;
int main()
{
format fmt("Name:%1% Age:%2%");
fmt % "TangYong";
fmt% "18";
string str = fmt.str();
cout << str.c_str()<<endl;
string str2 = (format("%1% %2% %3%") % "hello" % " world " % 520).str();
cout << str2 << endl;
system("pause");
return 0;
}