名稱空間
變體
操作

std::vformat_to

來自 cppreference.com
< cpp‎ | 工具‎ | 格式化
 
 
 
 
定義於標頭檔案 <format>
template< class OutputIt >
OutputIt vformat_to( OutputIt out, std::string_view fmt, std::format_args args );
(1) (C++20 起)
template< class OutputIt >
OutputIt vformat_to( OutputIt out, std::wstring_view fmt, std::wformat_args args );
(2) (C++20 起)
template< class OutputIt >

OutputIt vformat_to( OutputIt out, const std::locale& loc,

                     std::string_view fmt, std::format_args args );
(3) (C++20 起)
template< class OutputIt >

OutputIt vformat_to( OutputIt out, const std::locale& loc,

                     std::wstring_view fmt, std::wformat_args args );
(4) (C++20 起)

根據格式字串 fmt 格式化 args 中儲存的引數,並將結果寫入輸出迭代器 out。如果存在,loc 用於語言環境特定的格式化。

CharTdecltype(fmt)::char_type (對於過載 (1,3)char,對於過載 (2,4)wchar_t)。

這些過載只有在 OutputIt 滿足概念 std::output_iterator<const CharT&> 時才參與過載決議。

OutputIt 必須建模(滿足語義要求)概念 std::output_iterator<const CharT&>,並且 std::formatter<Ti, CharT> 必須滿足任何引數型別 TiFormatter 要求。否則,行為是未定義的。

目錄

[編輯] 引數

out - 指向輸出緩衝區的迭代器
fmt - 表示格式化字串的物件。格式化字串由以下部分組成:
  • 普通字元(除了 {}),它們不變地複製到輸出中,
  • 轉義序列 {{}},它們分別在輸出中替換為 {},以及
  • 替換欄位。

每個替換欄位具有以下格式:

{ arg-id (可選) } (1)
{ arg-id (可選) : format-spec } (2)
1) 沒有格式化規範的替換欄位
2) 帶有格式化規範的替換欄位
arg-id - 指定 args 中用於格式化的引數的索引;如果省略,則按順序使用引數。

格式化字串中的 arg-id 必須全部存在或全部省略。混合手動和自動索引是錯誤的。

format-spec - 由相應引數的 std::formatter 特化定義的格式規範。不能以 } 開頭。

(C++23 起)
(C++26 起)
  • 對於其他可格式化型別,格式化規範由使用者定義的 formatter 特化決定。
args - 要格式化的引數
loc - 用於特定於語言環境的格式化的 std::locale

[編輯] 返回值

輸出範圍末尾的迭代器。

[編輯] 異常

如果 fmt 對於提供的引數不是有效的格式字串,則丟擲 std::format_error。也會傳播由格式化器或迭代器操作丟擲的任何異常。

[編輯] 示例

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
P2216R3 C++20 args 的型別根據 OutputIt 進行引數化 不引數化

[編輯] 參見