Boost字符串处理
(1):Boost學習之格式化輸出--format:
原文鏈接:http://www.cnblogs.com/lzjsky/archive/2011/05/05/2037327.html
此文非常詳細!
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;}boost::format里的指示符語法大致有三大類:
繼承并強化自printf的格式化字符串
??? 形式為: [ N$ ] [ flags ] [ width ] [ . precision ] type-char??? N$可選,指定使用第N個參數(注意,要么所有指示符都加此參數,要么都不加)
??? 接下來的參數可以參數printf的指示符,只是format為其中的flags添加了'_'和'='標志,用于指出內部對齊和居中對齊。
設置打印規則,它是printf參數的一個補充,只是為了更直觀點。
??? 形式為: %|spec|??? 如:%|1$+5|表示顯示第一個參數,顯示正負號,寬度為5
簡單的位置標記
??? 形式為: %N%??? 簡單地聲明顯示第N個參數,優點是比較直觀而且不用指定類型。
(2):C/C++的格式化輸出(4)--boost::format
原文鏈接:http://blog.sina.com.cn/s/blog_a0d2e5590101ados.html
此文比較系統!
boost::format里的指示符語法大致有四大類:
繼承并強化自printf的格式化字符串
???形式為:[N$ ] [ flags ] [ width ] [ . precision ] type-char???N$可選,指定使用第N個參數(注意,要么所有指示符都加此參數,要么都不加)
???接下來的參數可以參數printf的指示符,只是format為其中的flags添加了'_'和'='標志,用于指出內部對齊和居中對齊。
設置打印規則,它是printf參數的一個補充,只是為了更直觀點。
???形式為:%|spec|???如:%|1$+5|表示顯示第一個參數,顯示正負號,寬度為5
簡單的位置標記
???形式為:%N%???簡單地聲明顯示第N個參數,優點是比較直觀而且不用指定類型。 boost::format新的格式說明符?
??
?%{nt}?
?當n是正數時,插入n個絕對制表符?
?cout <<boost::format("[t]")? <<endl;?
?%{nTX}?
?使用X做為填充字符代替當前流的填充字符(一般缺省是一個空格)?
?cout <<boost::format("[T*]")? <<endl;?
下面是轉載:http://www.cnblogs.com/WuErPIng/archive/2005/04/21/142308.html
淺嘗BOOST之FORMAT
概述?
?????std::string是個很不錯的東東,但實際使用時基本在每個程序里都會遇到不愉快的事情:格式化字符串。我甚至由于這個原因在代碼里引入平臺有關的MFC,ATL等本來不需要在項目中使用的一些重量級的框架,就為了能輕松的做格式化字符串:-)。曾嘗試過將ATL::CString的format函數提取出來使用,但ATL::CString的底層調用了windows獨有函數,無法跨越平臺。當然,現在有了boost::format,我們不用再擔心了。boost::format重載了'%'操作符,通過多次調用'%'操作符就能將參數非常方便格式化成字符串,并實現了ATL::CString和C#中的string兩者的格式化字符串功能。除了語法剛開始感覺到怪異,功能足以讓人感覺到興奮!?
一、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::formatformater("%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??
???
?
?cout?<<?boost::format("\n\n%s"?
?"%1t 十進制 =[%d]\n"?
?"%1t 格式化的十進制 =[]]\n"?
?"%1t 格式化十進制,前補'0' =[d]\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;?
?
?
輸出結果?
三、boost::format新的格式說明符?
??
?%{nt}?
?當n是正數時,插入n個絕對制表符?
?cout <<boost::format("[t]")? <<endl;?
??
?%{nTX}?
?使用X做為填充字符代替當前流的填充字符(一般缺省是一個空格)?
?cout <<boost::format("[T*]")? <<endl;??
四、異常處理?
?一般寫法:?
?{?
?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 werepassed?
?}?
?boost::format的文檔中有選擇處理異常的辦法,不過個人感覺實用性可能不強,下面是文檔中的例子??
??
?//?boost::io::too_many_args_bitselects errors due to passing too manyarguments.?
?//?boost::io::too_few_args_bitselects errors due to asking for the srting result before allarguments are passed?
?
?boost::formatmy_fmt(const?std::string?&?f_string)?
?{?
?using?namespace?boost::io;?
?formatfmter(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;
?
??
五、還有其它一些功能,但暫時感覺派不上用處,就不去深究了。
(3):char*,const?char*和string的相互轉換
原文鏈接:http://blog.sina.com.cn/s/blog_5436b2f40100pjzz.html
為了以后查找方便,特此總結如下。
如果有不對的地方或者有更簡單的方法,請指出~~
1.// string轉const char*string s = "abc";const char* c_s =s.c_str();2.// const char*轉string直接賦值即可const char* c_s ="abc";string s(c_s);3. //string轉char*string s = "abc";char* c;const int len =s.length();c = new char[len+1];strcpy(c,s.c_str());4. //char*轉stringchar* c = "abc";string s(c);5. //const char*轉char*const char* cpc ="abc";char* pc = newchar[100];//足夠長strcpy(pc,cpc);6. //char*轉const char*//直接賦值即可char* pc = "abc";const char* cpc = pc;(4):我的遍歷代碼
總結
以上是生活随笔為你收集整理的Boost字符串处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信开放境内各城市微信支付商户网络,20
- 下一篇: iON云服务器购买图文教程