std::to_chars
| 定義於標頭檔 <charconv> |
||
std::to_chars_result to_chars( char* first, char* last, |
(1) | (自 C++17 起) (C++23 起為 constexpr) |
| std::to_chars_result to_chars( char*, char*, bool, int = 10 ) = delete; |
(2) | (自 C++17 起) |
| std::to_chars_result to_chars( char* first, char* last, /* floating-point-type */ value ); |
(3) | (自 C++17 起) |
| std::to_chars_result to_chars( char* first, char* last, /* floating-point-type */ value, |
(4) | (自 C++17 起) |
| std::to_chars_result to_chars( char* first, char* last, /* floating-point-type */ value, |
(5) | (自 C++17 起) |
將 value 轉換為字串,並依序填入範圍 [first, last),其中 [first, last) 必須為有效的範圍。
10..35(含)內的數字表示為小寫字母 a..z。若數值小於零,則以負號開頭。程式庫為所有 cv 限定詞不修飾的(C++23 起)有號與無號整數型別,以及 char 型別的 value 參數提供了多載。std::to_chars 會拒絕 bool 型別的引數,因為若允許轉換,其結果應為 "0"/"1" 而非 "false"/"true"。目錄 |
[編輯] 參數
| first, last | - | 要寫入的字元範圍 |
| value | - | 要轉換為字串表示的數值 |
| base | - | 使用的整數進位:2 到 36 之間的值(含)。 |
| fmt | - | 使用的浮點格式,為 std::chars_format 型別的位元遮罩 |
| precision | - | 使用的浮點數精度 |
[編輯] 回傳值
成功時,回傳 std::to_chars_result 型別的值,其中 ec 等於值初始化的 std::errc,而 ptr 為寫入字元後的第一個位置指標。注意字串並「沒有」以 NUL 終止。
失敗時,回傳 std::to_chars_result 型別的值,其中 ec 包含 std::errc::value_too_large,ptr 為 last 的副本,並讓範圍 [first, last) 的內容處於未指定狀態。
[編輯] 例外
不會拋出例外。
[編輯] 備註
與 C++ 和 C 程式庫中的其他格式化函式不同,std::to_chars 是獨立於語系(locale)、無配置記憶體行為且不會拋出例外的。它僅提供其他程式庫(如 std::sprintf)所用格式化策略的一小部分子集。此設計旨在實現最高效的實作,以滿足常見的高吞吐量環境需求,例如文字交換(JSON 或 XML)。
關於 std::from_chars 能準確還原由 std::to_chars 格式化的每一種浮點數值的保證,僅在兩個函式來自同一實作時才成立。
若希望將 bool 值格式化為 "0"/"1",則必須顯式轉型為其他整數型別。
| 功能測試巨集 | 數值 | 標準 | 功能 |
|---|---|---|---|
__cpp_lib_to_chars |
201611L |
(C++17) | 基礎字串轉換 (std::to_chars, std::from_chars) |
202306L |
(C++26) | 測試 <charconv> 函式的成功或失敗 | |
__cpp_lib_constexpr_charconv |
202207L |
(C++23) | 為 std::to_chars 和 std::from_chars 的整數型別多載 (1) 新增 constexpr 修飾符 |
[編輯] 範例
#include <charconv> #include <iomanip> #include <iostream> #include <string_view> #include <system_error> void show_to_chars(auto... format_args) { const size_t buf_size = 10; char buf[buf_size]{}; std::to_chars_result result = std::to_chars(buf, buf + buf_size, format_args...); if (result.ec != std::errc()) std::cout << std::make_error_code(result.ec).message() << '\n'; else { std::string_view str(buf, result.ptr - buf); std::cout << std::quoted(str) << '\n'; } } int main() { show_to_chars(42); show_to_chars(+3.14159F); show_to_chars(-3.14159, std::chars_format::fixed); show_to_chars(-3.14159, std::chars_format::scientific, 3); show_to_chars(3.1415926535, std::chars_format::fixed, 10); }
可能輸出
"42" "3.14159" "-3.14159" "-3.142e+00" Value too large for defined data type
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯應用於之前的 C++ 標準。
| DR | 應用於 | 出版時的行為 | 正確的行為 |
|---|---|---|---|
| LWG 2955 | C++17 | 此函式原位於 <utility> 且使用 std::error_code | 移至 <charconv> 並使用 std::errc |
| LWG 3266 | C++17 | bool 引數曾被接受並提升為 int | 改為由刪除的多載拒絕 |
| LWG 3373 | C++17 | std::to_chars_result 可能有額外的成員 |
不允許額外的成員 |
[編輯] 參見
| (C++17) |
std::to_chars 的回傳型別 (類別) |
| (C++17) |
將字元序列轉換為整數或浮點數值 (函式) |
| (C++11) |
將整數或浮點數值轉換為 string(函式) |
| (C++11) |
將格式化輸出列印至 stdout、檔案串流或緩衝區 (函式) |
| 插入格式化數據 ( std::basic_ostream<CharT,Traits> 的公開成員函式) |