網站首頁 編程語言 正文
Boost.Format
Boost.Format 提供了函數 std::printf() 的替代品。 std::printf() 源自 C 標準并允許格式化數據輸出。但是,它既不是類型安全的,也不是可擴展的。 Boost.Format 提供了一種類型安全且可擴展的替代方案。
Boost.Format 提供了一個名為 boost::format 的類,該類在 boost/format.hpp 中定義。與 std::printf() 類似,將包含用于控制格式的特殊字符的字符串傳遞給 boost::format 的構造函數。替換輸出中這些特殊字符的數據通過運算符 operator% 鏈接。
示例 7.1。使用 boost::format 格式化輸出
#include <boost/format.hpp>
#include <iostream>
int main()
{
std::cout << boost::format{"%1%.%2%.%3%"} % 12 % 5 % 2014 << '\n';
}
Boost.Format 格式字符串使用放置在兩個百分號之間的數字作為實際數據的占位符,這些數據將使用 operator% 進行鏈接。示例 7.1 使用數字 12、5 和 2014 作為數據創建格式為 12.5.2014 的日期字符串。為了使月份出現在日期的前面,這在美國很常見,可以交換占位符。示例 7.2 進行了此更改,顯示 2014 年 5 月 12 日
示例 7.2。帶有 boost::format 的編號占位符
#include <boost/format.hpp>
#include <iostream>
int main()
{
std::cout << boost::format{"%2%/%1%/%3%"} % 12 % 5 % 2014 << '\n';
}
為了使用操縱器格式化數據,Boost.Format 提供了一個名為 boost::io::group() 的函數。
示例 7.3。使用帶有 boost::io::group() 的操縱器
#include <boost/format.hpp>
#include <iostream>
int main()
{
std::cout << boost::format{"%1% %2% %1%"} %
boost::io::group(std::showpos, 1) % 2 << '\n';
}
Example7.3
示例 7.3 對將與“%1%”關聯的值使用操縱器 std::showpos()。因此,此示例將顯示 +1 2 +1 作為輸出。因為操縱器 std::showpos() 已使用 boost::io::group() 鏈接到第一個數據值,所以只要顯示該值,就會自動添加加號。在這種情況下,格式占位符“%1%”會使用兩次。
如果加號僅應顯示為 1 的第一個輸出,則需要自定義格式占位符。
示例 7.4。帶有特殊字符的占位符
#include <boost/format.hpp>
#include <iostream>
int main()
{
std::cout << boost::format{"%|1$+| %2% %1%"} % 1 % 2 << '\n';
}
Example7.4
??? 示例 7.4 就是這樣做的。在此示例中,占位符“%1%”的第一個實例被替換為“%|1$+|”。格式的定制不僅僅是添加兩個額外的管道符號。對數據的引用也放在管道符號之間,并使用“1$”而不是“1%”。這是將輸出修改為 +1 2 1 所必需的。您可以在 Boost 文檔中找到有關格式規范的詳細信息。 必須為所有占位符或不指定對數據的占位符引用。示例 7.5 僅提供三個占位符之一的引用,這會在運行時生成錯誤。
示例 7.5。 boost::io::format_error 以防出錯
#include <boost/format.hpp>
#include <iostream>
int main()
{
try
{
std::cout << boost::format{"%|+| %2% %1%"} % 1 % 2 << '\n';
}
catch (boost::io::format_error &ex)
{
std::cout << ex.what() << '\n';
}
}
Example7.5
示例 7.5 引發了 boost::io::format_error 類型的異常。嚴格來說,Boost.Format 會拋出 boost::io::bad_format_string。但是,由于不同的異常類都是從 boost::io::format_error 派生的,因此通常更容易捕獲這種類型的異常。
示例 7.6 展示了如何在不使用格式字符串中的引用的情況下編寫程序。
示例 7.6。沒有數字的占位符
#include <boost/format.hpp>
#include <iostream>
int main()
{
std::cout << boost::format{"%|+| %|| %||"} % 1 % 2 % 1 << '\n';
}
在這種情況下,可以安全地省略第二個和第三個占位符的管道符號,因為它們沒有指定任何格式。生成的語法與 std::printf() 非常相似(參見示例 7.7)。
示例 7.7。 boost::format 使用來自 std::printf() 的語法
#include <boost/format.hpp>
#include <iostream>
int main()
{
std::cout << boost::format{"%+d %d %d"} % 1 % 2 % 1 << '\n';
}
雖然格式可能看起來像 std::printf() 使用的格式,但 Boost.Format 提供了類型安全的優勢。格式字符串中的字母“d”不表示數字的輸出。相反,它將操縱器 std::dec() 應用于 boost::format 使用的內部流對象。這使得指定對 std::printf() 沒有意義的格式字符串成為可能,并且會導致崩潰。
示例 7.8。 boost::format 帶有看似無效的占位符
#include <boost/format.hpp>
#include <iostream>
int main()
{
std::cout << boost::format{"%+s %s %s"} % 1 % 2 % 1 << '\n';
}
std::printf() 只允許 const char* 類型的字符串使用字母“s”。使用 std::printf() 時,“%s”和數值的組合會失敗。但是,示例 7.8 完美運行。 Boost.Format 不需要字符串。相反,它應用適當的操縱器來配置內部流。
Boost.Format 既是類型安全的又是可擴展的。只要運算符 operator<< 為 std::ostream 重載,任何類型的對象都可以與 Boost.Format 一起使用。
示例 7.9。 boost::format 具有用戶定義的類型
#include <boost/format.hpp>
#include <string>
#include <iostream>
struct animal
{
std::string name;
int legs;
};
std::ostream &operator<<(std::ostream &os, const animal &a)
{
return os << a.name << ',' << a.legs;
}
int main()
{
animal a{"cat", 4};
std::cout << boost::format{"%1%"} % a << '\n';
}
Example7.9
示例 7.9 使用 boost::format 將用戶定義類型動物的對象寫入標準輸出。這是可能的,因為流運算符對于動物來說是重載的。
原文鏈接:https://yamagota.blog.csdn.net/article/details/127279930
相關推薦
- 2022-04-25 C#用NPOI導出導入Excel幫助類_C#教程
- 2022-11-06 golang?RPC包原理和使用詳細介紹_Golang
- 2023-07-08 el-radio-group選中后再次點擊取消,及el-radio-button具有邊框懸浮問題解決
- 2021-12-04 Flutter實現頁面路由及404路由攔截_Android
- 2022-07-07 Python推導式使用詳情_python
- 2023-04-12 Docker?login和logout的使用_docker
- 2022-04-25 基于NPOI用C#開發的Excel以及表格設置_C#教程
- 2022-05-03 在Django中動態地過濾查詢集的實現_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支