boost format使用详解
簡述
編程中經常會遇到需要到使用的數據進行格式化的地方,如生成記錄日志的字符串,生成發送到網絡的完整數據等。
可能需要對int, string, char等進行格式化,常用的方法包括 snprintf, std::string, std::stringstream, boost::format等。
snprintf支持c語言,可能是這幾種方式中效率最高的,它們之間的效率對比可以參考 Comparing the performance of a sequence of several generators 。
但對于效率要求不是非常嚴苛的場合下,使用其他方法可能會更加方便,這里就要介紹boost::format。
boost::format使用
如果你已經在使用boost庫,使用format進行格式化將是一件非常簡單的事情。
如前所述,format沒有snprintf快,但它是類型安全的。它使用streams構建輸出,所以不會存在內存溢出的情況。
format有兩種使用風格,一種類似于 printf,另一種使用占位符,類似于c#。
浮點數格式
直接上代碼:
const double almostpi = 22.0 / 7.0;// Printf printf("Pi is %f\n", almostpi);// Boost format with printf syntax boost::format printf_formatting("Pi is %f\n"); std::cout << printf_formatting % almostpi;// Boost format with positional syntax boost::format position_formatting("Pi is %1%\n"); std::cout << position_formatting % almostpi;// Output: // Pi is 3.142857 // Pi is 3.142857 // Pi is 3.14286顯然,format的使用與printf類似,使用 %f 格式化一個浮點數,默認的精度是6位小數。
使用更大的數據可以驗證這個結論,如:
const double almostpi = 22.0 / 7.0; const double distancetosun = 149600000000.0; // meters const double earthorbitlength = 2 * almostpi * distancetosun;// Printf printf("Earth orbit distance is %f meters\n", earthorbitlength);// Boost format with printf syntax boost::format printf_formatting("Earth orbit distance is %f meters\n"); std::cout << printf_formatting % earthorbitlength;// Boost format with positional syntax boost::format position_formatting("Earth orbit distance is %1% meters\n"); std::cout << position_formatting % earthorbitlength;// Output: // Earth orbit distance is 940342857142.857180 meters // Earth orbit distance is 940342857142.857180 meters // Earth orbit distance is 9.40343e+011 meters使得 %1% 占位符時,當數值較大時,會使用科學計數法(大概6-7個字符長度時會使用)。
關于科學計數法
一般而言,只有在使用占位符時,且數值較大時才會轉換為科學計數法。如:
boost::format scinotation("Do we use scientific? '%1%'\n");// %llu時也會顯示科學計數法for (size_t n=0; n<15; ++n) {double value = 1 * pow(10, n) + 0.5;std::cout << scinotation % value; }// Output: // Do we use scientific? '1.5' // Do we use scientific? '10.5' // Do we use scientific? '100.5' // Do we use scientific? '1000.5' // Do we use scientific? '10000.5' // Do we use scientific? '100001' // Do we use scientific? '1e+006' // Do we use scientific? '1e+007' // Do we use scientific? '1e+008' // Do we use scientific? '1e+009' // Do we use scientific? '1e+010' // Do we use scientific? '1e+011' // Do we use scientific? '1e+012' // Do we use scientific? '1e+013' // Do we use scientific? '1e+014'另外,在實際使用中,如果使用了不匹配的標記符時,如使用了%llu, 而實際數值卻是double時,也會以科學計數法顯示。
可以使用 %e 顯示指定以科學計數法顯示。
格式控制
格式控制的語法為:%[N$][flags][width][.precision]type-char。如:
// Printf printf("Pi is '%10.2f'\n", almostpi);// Boost format with printf syntax boost::format printf_formatting("Pi is '%10.2f'\n"); std::cout << printf_formatting % almostpi;// Boost format with positional syntax boost::format position_formatting("Pi is '%1$10.2f'\n"); std::cout << position_formatting % almostpi;// Output: // Pi is ' 3.14' // Pi is ' 3.14' // Pi is ' 3.14'常用示例
簡單的使用方式,可以直接輸出想要的內容:
// 直接輸出 cout << boost::format("%s") % "this is what i want" << endl; // 結合 string string s; s = str(boost::format("%s") % "this is what i want"); cout << s << endl;// 使用 formater boost::format fmt("%s"); fmt % "this is what i want"; string s = fmt.str(); cout << s << endl;// 占位符 cout << boost::format("%1%") % "this is what i want" << endl;異常處理
format 異常時會throw 異常信息,所以要使用try語句塊處理異常,否則程序默認會終止。如:
try {cout << boost::format("%d%d") % 1 << endl; } catch (std::exception const &e) {cout << e.what() << endl; } catch (...) {cout << "format error" << endl; }// output // boost::too_few_args: format-string refered to more arguments than were passed參考資料:
Boost::format (and wformat) examples – numbers: int, float, double
Comparing the performance of a sequence of several generators
淺嘗boost之format
總結
以上是生活随笔為你收集整理的boost format使用详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【怎么用u盘装系统】
- 下一篇: ubuntu下安装小企鹅输入法