boost::format类提供了类似C语言里'printf'功能的格式化输出能力,当然功能更强大。
所需头文件:
#include <boost/format.hpp>
示例代码:
#include <iostream> #include <string> #include <boost/format.hpp> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { // 使用%序号%的方式给出指示符, 后面用%连接对应的数据。 cout << boost::format("writing %1%, x=%2% : %3%-th try") % "toto" % 40.23 % 50 << endl; // 输出:writing toto, x=40.23 : 50-th try // 可以延迟使用,顺序不必一致 boost::format fmter("%2% %1%"); fmter % 36; fmter % 77; cout << fmter << endl; // 输出:77 36 // 可重用 fmter % 12; fmter % 24; cout << fmter << endl; // 输出:24 12 // 可直接转成字符串 std::string s = fmter.str(); std::string s2 = str( boost::format("%2% %1% %2% %1%")%"World"%"Hello"); cout << s << endl << s2 << endl; // 输出: // 24 12 // Hello World Hello World // 可以使用printf指示符 cout << boost::format("%3.1f - %.2f%%") % 10.0 % 12.5 << endl; // 输出:10.0 - 12.50% // printf指示符里使用N$指定使用第几个参数 cout << boost::format("%2$3.1f - %1$.2f%%") % 10.0 % 12.5 << endl; // 输出:12.5 - 10.00% cin.get(); return 0; }
// ATL::CString风格 cout << boost::format("\n\n%s" "%1t 十进制 = [%d]\n" "%1t 格式化的十进制 = [%5d]\n" "%1t 格式化十进制,前补'0' = [%05d]\n" "%1t 十六进制 = [%x]\n" "%1t 八进制 = [%o]\n" "%1t 浮点 = [%f]\n" "%1t 格式化的浮点 = [%3.3f]\n" "%1t 科学计数 = [%e]\n" ) % "example :\n" % 15 % 15 % 15 % 15 % 15 % 15.01 % 15.01 % 15.01 << endl; // C#::string风格 cout << boost::format("%1%" "%1t 十进制 = [%2$d]\n" "%1t 格式化的十进制 = [%2$5d]\n" "%1t 格式化十进制,前补'0' = [%2$05d]\n" "%1t 十六进制 = [%2$x]\n" "%1t 八进制 = [%2$o]\n" "%1t 浮点 = [%3$f]\n" "%1t 格式化的浮点 = [%3$3.3f]\n" "%1t 科学计数 = [%3$e]\n" ) % "example :\n" % 15 % 15.01 << endl; 输出结果 /* example : 十进制 = [15] 格式化的十进制 = [ 15] 格式化十进制,前补'0' = [00015] 十六进制 = [f] 八进制 = [17] 浮点 = [15.010000] 格式化的浮点 = [15.010] 科学计数 = [1.501000e+001] */
boost::format里的指示符语法大致有三大类:
继承并强化自printf的格式化字符串
形式为:[ N$ ] [ flags ] [ width ] [ . precision ] type-charN$可选,指定使用第N个参数(注意,要么所有指示符都加此参数,要么都不加)
接下来的参数可以参数printf的指示符,只是format为其中的flags添加了'_'和'='标志,用于指出内部对齐和居中对齐。
设置打印规则,它是printf参数的一个补充,只是为了更直观点。
形式为:%|spec|如:%|1$+5|表示显示第一个参数,显示正负号,宽度为5
简单的位置标记
形式为:%N%简单地声明显示第N个参数,优点是比较直观而且不用指定类型。
------------------------------------------------------------------------------------------------------------------------------------------------
Boost库的format提供了类似于C标准库函数printf的格式化字符串功能,并且它是强类型安全的。
由于format重载了%操作符,因此,通过单次或多次调用%操作符可以非常方便地格式化字符串。
format的基本语法结构为:format(需要格式化的字符串 ) %参数1 %参数2 …%参数n。
format的基本语法结构为:format(需要格式化的字符串 ) %参数1 %参数2 …%参数n。
下面介绍format的几种使用方法。
1. 直接使用format
cout << format("%2% and %1% and %2%")%16 %"hello" << endl;
其中%1%表示第一个参数,%2%表示第二个参数,其他依此类推。另外,参数是可以复用的。
2. 使用format的str函数
format fmt("%2% and %1%");
fmt %16 %"hello";
string s = fmt.str();
cout << s << endl;
3. 使用全局的str函数
format fmt("%2% and %1%");
fmt %16 %"hello";
string s = str(fmt) ;
cout << s << endl;
4. 多次调用%操作符
format fmt("%2% and %1%");
fmt %16;
fmt %"hello";
cout << fmt << endl;
5. 使用printf风格
cout << format("%d and %s")%16 %"hello" << endl;
6. 使用混合风格
cout << format("%2$s and %1$d")%16 %"hello" << endl;
其中%后面是参数的序号,$后面是printf风格的格式符。
另外,format还提供了一些其他的格式说明符。
比如:%nt用于产生一个固定在第n列的\t;%nTx可以将x作为填充字符以代替当前流的填充字符(默认是一个空格)。
当实际传入的参数个数与格式化字符串中需要的参数个数不一致时,format会抛出异常。
举例如下:
cout << format("[abc%10t]") << format("[%10T*]") << endl;
try
{
cout << format("%1% and %2%") %16 << endl;
}
catch (exception &e)
{
cout << e.what() << endl;
}
cout << format("[abc%10t]") << format("[%10T*]") << endl;
try
{
cout << format("%1% and %2%") %16 << endl;
}
catch (exception &e)
{
cout << e.what() << endl;
}
最后谈一下使用format时需要注意的两点。
一是使用默认的format风格(如%1%)输出浮点数类型时,只是将浮点数转换为对应的字符串,无法保证精度,如果需要保证精度,可以使用printf风格(如%f)输出浮点数类型;
二是format不支持直接使用CString作为参数,需要先将CString转换为字符指针(如format("%1% ") %(LPCTSTR)szText)。
                    
                
                
            
        
浙公网安备 33010602011771号