Boost::Format使用指南。

來源:互聯網
上載者:User

盡量少用sprintf了,什麼時候程式掛了都不知道。

http://www.cnblogs.com/WuErPIng/archive/2005/04/21/142308.aspx

淺嘗boost之format

 

 

一、boost::format工作的方式
 
 基本的文法,boost::format( format-string ) % arg1 % arg2 % ... % argN
 
 下面的例子說明boost::format簡單的工作方式 
  
 

// 方式一
cout << boost::format("%s") % "輸出內容" << endl;

// 方式二
std::string s;
s = str( boost::format("%s") % "輸出內容" );
cout << s << endl;

// 方式三
boost::format formater("%s");
formater % "輸出內容";
std::string s = formater.str();
cout << s << endl;

// 方式四
cout << boost::format("%1%") % boost::io::group(hex, showbase, 40) << endl;

二、boost::format實際使用的執行個體
 
 格式化文法: [ N$ ] [ flags ] [ width ] [ . precision ] type-char 
  
 

// 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]
*/

上邊的%1t表示tab操作符,無需對應輸入參數,
每一對“”見的東東基本上對應一個輸入參數。%d,%f,%x,等對應了輸入的類型,注意,一個“”裡面只能對應一個輸入類型。。。呵呵。
三、boost::format新的格式說明符
 
 %{nt}
 當n是正數時,插入n個絕對定位字元
 cout << boost::format("[%10t]")  << endl;
 
 %{nTX}
 使用X做為填充字元代替當前流的填充字元(一般預設是一個空格)
 cout << boost::format("[%10T*]")  << endl; 

四、異常處理

 一般寫法:

try
{
cout << boost::format("%d%d") % 1 << endl;
}
catch(std::exception const & e)
{
cout << e.what() << endl;

// 輸出內容:
// boost::too_few_args: format-string refered to more arguments than were passed
}

 boost::format的文檔中有選擇處理異常的辦法,不過個人感覺實用性可能不強,下面是文檔中的例子 
 

// boost::io::all_error_bits selects all errors
// 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;

 

 
五、還有其它一些功能,但暫時感覺派不上用處,就不去深究了。

 

 

概述

     std::string是個很不錯的東東,但實際使用時基本在每個程式裡都會遇到不愉快的事情:格式化字串。我甚至由於這個原因在代碼裡引入平台有關的MFC,ATL等本來不需要在項目中使用的一些重量級的架構,就為了能輕鬆的做格式化字串 。曾嘗試過將ATL::CString的format函數提取出來使用,但ATL::CString的底層調用了windows專屬函數,無法跨越平台。當然,現在有了boost::format,我們不用再擔心了。boost::format重載了'%'操作符,通過多次調用'%'操作符就能將參數非常方便格式化成字串,並實現了ATL::CString和C#中的string兩者的格式化字串功能。除了文法剛開始感覺到怪異,功能足以讓人感覺到興奮!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.