名稱空間
變體
操作

std::to_chars

來自 cppreference.com
< cpp‎ | 工具
定義於標頭檔案 <charconv>
std::to_chars_result

    to_chars( char* first, char* last,

              /* integer-type */ value, int base = 10 );
(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,

              std::chars_format fmt );
(4) (C++17 起)
std::to_chars_result

    to_chars( char* first, char* last, /* floating-point-type */ value,

              std::chars_format fmt, int precision );
(5) (C++17 起)

透過依次填充範圍 [firstlast)value 轉換為字串,其中 [firstlast) 必須是有效範圍

1) 整數格式化器:value 被轉換為給定 base (不帶冗餘前導零)的數字字串。範圍 10..35(含)中的數字表示為小寫字元 a..z。如果 value 小於零,則表示以減號開頭。庫為所有 cv 非限定(C++23 起)有符號和無符號整數型別以及型別 char 作為引數 value 的型別提供了過載。
2) bool 的過載被刪除。std::to_chars 拒絕型別為 bool 的引數,因為如果允許,結果將是 "0"/"1" 而不是 "false"/"true"
3) value 被轉換為字串,就像在預設("C")區域設定中使用 std::printf 一樣。轉換說明符是 fe(在平局情況下傾向於 f),根據最短表示的要求選擇:字串表示由最小數量的字元組成,使得在小數點(如果存在)之前至少有一個數字,並且使用相應的 std::from_chars 函式解析表示能夠精確地恢復 value。如果存在多個這樣的表示,則選擇與 value 差異最小的那個,並使用 std::round_to_nearest 的舍入規則解決任何剩餘的平局。庫為所有 cv 非限定 標準(C++23 前)浮點型別作為引數 value 的型別提供了過載。
4)(3) 相同,但是如果 fmtstd::chars_format::fixed,則 as-if printf 指定的轉換是 f;如果 fmtstd::chars_format::scientific,則為 e;如果 fmtstd::chars_format::hex,則為 a(但結果中沒有前導 "0x");如果 fmtchars_format::general,則為 g。庫為所有 cv 非限定 標準(C++23 前)浮點型別作為引數 value 的型別提供了過載。
5)(4) 相同,除了精度由引數 precision 指定,而不是由最短表示要求指定。庫為所有 cv 非限定 標準(C++23 前)浮點型別作為引數 value 的型別提供了過載。

目錄

[編輯] 引數

first, last - 要寫入的字元範圍
value - 要轉換為其字串表示的值
base - 要使用的整數基數:2 到 36(含)之間的值。
fmt - 要使用的浮點格式,型別為 std::chars_format 的位掩碼
precision - 要使用的浮點精度

[編輯] 返回值

成功時,返回型別為 std::to_chars_result 的值,其中 ec 等於值初始化的 std::errcptr 是寫入字元的尾後指標。請注意,字串沒有 NUL 終止。

錯誤時,返回型別為 std::to_chars_result 的值,其中 ecstd::errc::value_too_largeptrlast 的副本,並使範圍 [firstlast) 的內容處於未指定狀態。

[編輯] 異常

不丟擲任何異常。

[編輯] 註解

與 C++ 和 C 庫中的其他格式化函式不同,std::to_chars 是獨立於區域設定的,不進行分配,也不丟擲異常。它只提供了其他庫(如 std::sprintf)使用的一小部分格式化策略。這旨在實現最快的可能實現,適用於常見的吞吐量上下文,例如基於文字的交換 (JSONXML)。

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) 為整數型別新增 constexpr 修飾符到 std::to_charsstd::from_chars 過載 (1)

[編輯] 示例

#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++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
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 可能有額外的成員 禁止額外的成員

[編輯] 參閱

std::to_chars 的返回型別
(類) [編輯]
將字元序列轉換為整數或浮點值
(函式) [編輯]
(C++11)
將整數或浮點值轉換為 string
(函式) [編輯]
將格式化輸出列印到 stdout、檔案流或緩衝區
(函式) [編輯]
插入格式化資料
(std::basic_ostream<CharT,Traits> 的公有成員函式) [編輯]