std::vformat
來自 cppreference.com
定義於標頭檔案 <format> |
||
std::string vformat( std::string_view fmt, std::format_args args ); |
(1) | (C++20 起) |
std::wstring vformat( std::wstring_view fmt, std::wformat_args args ); |
(2) | (C++20 起) |
std::string vformat( const std::locale& loc, std::string_view fmt, std::format_args args ); |
(3) | (C++20 起) |
std::wstring vformat( const std::locale& loc, std::wstring_view fmt, std::wformat_args args ); |
(4) | (C++20 起) |
根據格式字串 fmt 格式化 args 中儲存的引數,並將結果作為字串返回。如果存在,loc 用於特定於語言環境的格式化。
目錄 |
[編輯] 引數
fmt | - | 表示格式化字串的物件。格式化字串由以下部分組成:
每個替換欄位具有以下格式:
1) 沒有格式化規範的替換欄位
2) 帶有格式化規範的替換欄位
| ||||||||||||||||||||||||||||||||||||||||||||||
args | - | 要格式化的引數 | ||||||||||||||||||||||||||||||||||||||||||||||
loc | - | 用於特定於語言環境的格式化的 std::locale |
[編輯] 返回值
一個包含格式化結果的字串物件。
[編輯] 異常
如果 fmt 對於提供的引數不是有效的格式字串,則丟擲 std::format_error,或者在分配失敗時丟擲 std::bad_alloc。也會傳播由格式化器或迭代器操作丟擲的任何異常。
[編輯] 示例
執行此程式碼
#include <format> #include <iostream> template<typename... Args> inline void println(const std::format_string<Args...> fmt, Args&&... args) { std::cout << std::vformat(fmt.get(), std::make_format_args(args...)) << '\n'; } int main() { println("{}{} {}{}", "Hello", ',', "C++", -1 + 2 * 3 * 4); }
輸出
Hello, C++23