日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

boost format使用详解

發(fā)布時間:2023/12/18 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 boost format使用详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

簡述


編程中經(jīng)常會遇到需要到使用的數(shù)據(jù)進行格式化的地方,如生成記錄日志的字符串,生成發(fā)送到網(wǎng)絡(luò)的完整數(shù)據(jù)等。

可能需要對int, string, char等進行格式化,常用的方法包括 snprintf, std::string, std::stringstream, boost::format等。

snprintf支持c語言,可能是這幾種方式中效率最高的,它們之間的效率對比可以參考 Comparing the performance of a sequence of several generators 。

但對于效率要求不是非常嚴(yán)苛的場合下,使用其他方法可能會更加方便,這里就要介紹boost::format。

boost::format使用


如果你已經(jīng)在使用boost庫,使用format進行格式化將是一件非常簡單的事情。

如前所述,format沒有snprintf快,但它是類型安全的。它使用streams構(gòu)建輸出,所以不會存在內(nèi)存溢出的情況。

format有兩種使用風(fēng)格,一種類似于 printf,另一種使用占位符,類似于c#。

浮點數(shù)格式

直接上代碼:

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 格式化一個浮點數(shù),默認(rèn)的精度是6位小數(shù)。

使用更大的數(shù)據(jù)可以驗證這個結(jié)論,如:

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% 占位符時,當(dāng)數(shù)值較大時,會使用科學(xué)計數(shù)法(大概6-7個字符長度時會使用)。

關(guān)于科學(xué)計數(shù)法

一般而言,只有在使用占位符時,且數(shù)值較大時才會轉(zhuǎn)換為科學(xué)計數(shù)法。如:

boost::format scinotation("Do we use scientific? '%1%'\n");// %llu時也會顯示科學(xué)計數(shù)法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'

另外,在實際使用中,如果使用了不匹配的標(biāo)記符時,如使用了%llu, 而實際數(shù)值卻是double時,也會以科學(xué)計數(shù)法顯示。

可以使用 %e 顯示指定以科學(xué)計數(shù)法顯示。

格式控制

格式控制的語法為:%[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'

常用示例


簡單的使用方式,可以直接輸出想要的內(nèi)容:

// 直接輸出 cout << boost::format("%s") % "this is what i want" << endl; // 結(jié)合 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語句塊處理異常,否則程序默認(rèn)會終止。如:

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

總結(jié)

以上是生活随笔為你收集整理的boost format使用详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。