網(wǎng)站首頁 編程語言 正文
用boost::format來格式化字符串
在字符串處理中少不了格式化字符串,C++中傳統(tǒng)的格式化函數(shù)是C語言的sprintf,但它一個很大的問題就是不安全。因此,在stl中引入了stringstream來實現(xiàn)安全格式化,但是stringstream卻遠(yuǎn)不如sprintf來得直觀。例如,對如如下代碼:
char text[]="hello";
bool is_all_lower = boost::algorithm::all(text, is_lower());
char output[128];
sprintf(output, "<%s> %s in the lower case", text, (is_all_lower? "is": "is not"));
如果把最后兩句format的函數(shù)用stringstream來寫的話,可讀性是遠(yuǎn)不如sprintf的。
stringstream output;
output << "<" << text << "> "
<< (is_all_lower)? "is": "is not")
<< " in the lower case";
boost引入了一個提供類似.net中的string.format的方式提供格式化字符串的函數(shù),用它來格式化的話就是如下形式:
boost::format fmt = boost::format("<%s> %s in the lower case") % text % (is_all_lower? "is": "is not");
string output = fmt.str();
前面的例子中演示的是C風(fēng)格的格式化字符串,boost.format也提供了類似.net風(fēng)格的格式化字符串方式:
boost::format fmt = boost::format("<%1%> %2% in the lower case") % text % (is_all_lower? "is": "is not");
cout << fmt << endl;
這種方式更容易看到參數(shù)在格式化字符串中的位置,推薦這種形式。不過它的起始坐標(biāo)是1而不是0,用慣了.net的string.format的朋友需要注意下。
格式化控制
格式化語法為: [ N$ ] [ flags ] [ width ] [ . precision ] type-char。也提供了C語言和.net兩種風(fēng)格。
//傳統(tǒng)c語言風(fēng)格
cout << boost::format("\n\n%s"
"%1t 十進(jìn)制 = [%d]\n"
"%1t 格式化的十進(jìn)制 = [%5d]\n"
"%1t 格式化十進(jìn)制,前補'0' = [%05d]\n"
"%1t 十六進(jìn)制 = [%x]\n"
"%1t 八進(jìn)制 = [%o]\n"
"%1t 浮點 = [%f]\n"
"%1t 格式化的浮點 = [%3.3f]\n"
"%1t 科學(xué)計數(shù) = [%e]\n"
) % "example :\n" % 15 % 15 % 15 % 15 % 15 % 15.01 % 15.01 % 15.01 << endl;
//.net的風(fēng)格
cout << boost::format("%1%"
"%1t 十進(jìn)制 = [%2$d]\n"
"%1t 格式化的十進(jìn)制 = [%2$5d]\n"
"%1t 格式化十進(jìn)制,前補'0' = [%2$05d]\n"
"%1t 十六進(jìn)制 = [%2$x]\n"
"%1t 八進(jìn)制 = [%2$o]\n"
"%1t 浮點 = [%3$f]\n"
"%1t 格式化的浮點 = [%3$3.3f]\n"
"%1t 科學(xué)計數(shù) = [%3$e]\n"
) % "example :\n" % 15 % 15.01 << endl;
異常處理
既然boost.format函數(shù)是用來代替sprintf的,那么自然就得有異常處理的功能,而不是像sprintf那樣死給你看。boost.format的處理方法是拋異常,它在如下兩種情況家會拋異常:
format字符串非法
format綁定非法
如下代碼演示了這兩種情形:
try
{
boost::format("<%3");
}
catch(std::exception& err)
{
cout << err.what() << endl;
}
boost::format fmt = boost::format("<%3%> %2% in the lower case") % text % (is_all_lower? "is": "is not");
try
{
cout << fmt << endl;
}
catch(std::exception& err)
{
cout << err.what() << endl;
}
封裝
boost.format是以一個對象,而不是函數(shù)來實現(xiàn)的,導(dǎo)致其使用和異常處理起來要麻煩不少,不過,利用c++11的可變參數(shù)模板的語法還是可以很容易把它封裝成一個可變參數(shù)的函數(shù)的形式:
string string_fromat(const char* format, …)
需要定義三個重載版本:
template<class TFirst>
void string_format(boost::format& fmt, TFirst&& first)
{
fmt % first;
}
template<class TFirst, class... TOther>
void string_format(boost::format& fmt, TFirst&& first, TOther&&... other)
{
fmt % first;
string_format(fmt, other...);
}
template<class TFirst, class... TOther>
string string_format(const char* format, TFirst&& first, TOther&&... other)
{
boost::format fmt(format);
string_format(fmt, first, other...);
return fmt.str();
}
現(xiàn)在就可以這么用了:
auto?output = string_format("<%1%> %2% in the lower case", text, (is_all_lower??"is":?"is not"));
所有的異常也都會在該函數(shù)中拋出,雖然效率上相對低點,但用起來要舒服點。
原文鏈接:https://www.cnblogs.com/TianFang/archive/2013/02/04/2891501.html
相關(guān)推薦
- 2022-09-14 Python最好的日期處理庫pendulum的使用指南_python
- 2022-11-19 如何使用?Go?和?Excelize?構(gòu)建電子表格_Golang
- 2022-08-30 python中Requests請求的安裝與常見用法_python
- 2022-11-17 Python?pyecharts模塊安裝與入門教程_python
- 2022-06-12 C語言?智能指針?shared_ptr?和?weak_ptr_C 語言
- 2023-07-27 element plus tabel 全選 保持?jǐn)?shù)據(jù)
- 2022-06-01 聊聊.Net,Core配置Nlog.md的問題_實用技巧
- 2022-12-08 c++只保留float型的小數(shù)點后兩位問題_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支