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;
}

Boost::Format是C++中一个强大的格式化库,它提供了类似于C的printf功能,同时结合了C++的iostream优点。程序员可以方便地进行字符串格式化,如示例所示,创建并使用format对象来插入变量到字符串模板中,然后转换为string进行输出。
200

被折叠的 条评论
为什么被折叠?



