浅尝boost之format
From: http://www.cnblogs.com/WuErPIng/archive/2005/04/21/142308.html
?
概述?
?????std::string是個很不錯的東東,但實(shí)際使用時基本在每個程序里都會遇到不愉快的事情:格式化字符串。我甚至由于這個原因在代碼里引入平臺有關(guān)的MFC,ATL等本來不需要在項(xiàng)目中使用的一些重量級的框架,就為了能輕松的做格式化字符串 :-) 。曾嘗試過將ATL::CString的format函數(shù)提取出來使用,但ATL::CString的底層調(diào)用了windows獨(dú)有函數(shù),無法跨越平臺。當(dāng)然,現(xiàn)在有了boost::format,我們不用再擔(dān)心了。boost::format重載了'%'操作符,通過多次調(diào)用'%'操作符就能將參數(shù)非常方便格式化成字符串,并實(shí)現(xiàn)了ATL::CString和C#中的string兩者的格式化字符串功能。除了語法剛開始感覺到怪異,功能足以讓人感覺到興奮!?
一、boost::format工作的方式?
??
?基本的語法,boost::format( format-string ) % arg1 % arg2 % ... % argN?
??
?下面的例子說明boost::format簡單的工作方式??
???
?
?cout?<<?boost::format("%s")?%?"輸出內(nèi)容"?<<?endl;?
?
?//?方式二?
?std::string?s;?
?s?=?str( boost::format("%s")?%?"輸出內(nèi)容"?);?
?cout?<<?s?<<?endl;?
?
?//?方式三?
?boost::format formater("%s");?
?formater?%?"輸出內(nèi)容";?
?std::string?s?=?formater.str();?
?cout?<<?s?<<?endl;?
?
?//?方式四?
?cout?<<?boost::format("%1%")?%?boost::io::group(hex, showbase,?40)?<<?endl;?
二、boost::format實(shí)際使用的實(shí)例?
??
?格式化語法: [ N$ ] [ flags ] [ width ] [ . precision ] type-char??
???
?
?cout?<<?boost::format("\n\n%s"?
?"%1t 十進(jìn)制 = [%d]\n"?
?"%1t 格式化的十進(jìn)制 = [%5d]\n"?
?"%1t 格式化十進(jìn)制,前補(bǔ)'0' = [%05d]\n"?
?"%1t 十六進(jìn)制 = [%x]\n"?
?"%1t 八進(jìn)制 = [%o]\n"?
?"%1t 浮點(diǎn) = [%f]\n"?
?"%1t 格式化的浮點(diǎn) = [%3.3f]\n"?
?"%1t 科學(xué)計(jì)數(shù) = [%e]\n"?
?)?%?"example :\n"?%?15?%?15?%?15?%?15?%?15?%?15.01?%?15.01?%?15.01?<<?endl;?
?
?//?C#::string風(fēng)格?
?cout?<<?boost::format("%1%"?
?"%1t 十進(jìn)制 = [%2$d]\n"?
?"%1t 格式化的十進(jìn)制 = [%2$5d]\n"?
?"%1t 格式化十進(jìn)制,前補(bǔ)'0' = [%2$05d]\n"?
?"%1t 十六進(jìn)制 = [%2$x]\n"?
?"%1t 八進(jìn)制 = [%2$o]\n"?
?"%1t 浮點(diǎn) = [%3$f]\n"?
?"%1t 格式化的浮點(diǎn) = [%3$3.3f]\n"?
?"%1t 科學(xué)計(jì)數(shù) = [%3$e]\n"?
?)?%?"example :\n"?%?15?%?15.01?<<?endl;?
?
?
輸出結(jié)果?
/*?
example :?
?十進(jìn)制 = [15]?
?格式化的十進(jìn)制 = [ 15]?
?格式化十進(jìn)制,前補(bǔ)'0' = [00015]?
?十六進(jìn)制 = [f]?
?八進(jìn)制 = [17]?
?浮點(diǎn) = [15.010000]?
?格式化的浮點(diǎn) = [15.010]?
?科學(xué)計(jì)數(shù) = [1.501000e+001]?
*/?
三、boost::format新的格式說明符?
??
?%{nt}?
?當(dāng)n是正數(shù)時,插入n個絕對制表符?
?cout << boost::format("[%10t]")? << endl;?
??
?%{nTX}?
?使用X做為填充字符代替當(dāng)前流的填充字符(一般缺省是一個空格)?
?cout << boost::format("[%10T*]")? << endl;??
四、異常處理?
?一般寫法:?
?{?
?cout?<<?boost::format("%d%d")?%?1?<<?endl;?
?}?
?catch(std::exception?const?&?e)?
?{?
?cout?<<?e.what()?<<?endl;?
?
?//?輸出內(nèi)容:?
?//?boost::too_few_args: format-string refered to more arguments than were passed?
?}?
?boost::format的文檔中有選擇處理異常的辦法,不過個人感覺實(shí)用性可能不強(qiáng),下面是文檔中的例子??
??
?//?boost::io::too_many_args_bit selects errors due to passing too many arguments.?
?//?boost::io::too_few_args_bit selects errors due to asking for the srting result before all arguments are passed?
?
?boost::format my_fmt(const?std::string?&?f_string)?
?{?
?using?namespace?boost::io;?
?format fmter(f_string);?
?fmter.exceptions( all_error_bits?^?( too_many_args_bit?|?too_few_args_bit ) );?
?return?fmter;?
?}?
?cout?<<?my_fmt("?%1% %2% \n")?%?1?%?2?%?3?%?4?%?5;
??
五、還有其它一些功能,但暫時感覺派不上用處,就不去深究了。
?
總結(jié)
以上是生活随笔為你收集整理的浅尝boost之format的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu server修改系统时区和
- 下一篇: android 代码混淆模板