名稱空間
變體
操作

std::to_string

來自 cppreference.com
< cpp‎ | string‎ | basic_string
 
 
 
std::basic_string
 
定義於標頭檔案 <string>
std::string to_string( int value );
(1) (C++11 起)
std::string to_string( long value );
(2) (C++11 起)
std::string to_string( long long value );
(3) (C++11 起)
std::string to_string( unsigned value );
(4) (C++11 起)
std::string to_string( unsigned long value );
(5) (C++11 起)
std::string to_string( unsigned long long value );
(6) (C++11 起)
std::string to_string( float value );
(7) (C++11 起)
std::string to_string( double value );
(8) (C++11 起)
std::string to_string( long double value );
(9) (C++11 起)

將數值轉換為 std::string

buf 為轉換函式內部的緩衝區,其大小足以容納轉換結果。

1) 將有符號整數轉換為字串,如同透過 std::sprintf(buf, "%d", value)
2) 將有符號整數轉換為字串,如同透過 std::sprintf(buf, "%ld", value)
3) 將有符號整數轉換為字串,如同透過 std::sprintf(buf, "%lld", value)
4) 將無符號整數轉換為字串,如同透過 std::sprintf(buf, "%u", value)
5) 將無符號整數轉換為字串,如同透過 std::sprintf(buf, "%lu", value)
6) 將無符號整數轉換為字串,如同透過 std::sprintf(buf, "%llu", value)
7,8) 將浮點值轉換為字串,如同透過 std::sprintf(buf, "%f", value)
9) 將浮點值轉換為字串,如同透過 std::sprintf(buf, "%Lf", value)
(直到 C++26)
1-9) 將數值轉換為字串,如同透過 std::format("{}", value)
(C++26 起)

目錄

[edit] 引數

value - 要轉換的數值

[edit] 返回值

一個包含轉換後值的字串。

[edit] 異常

可能會從 std::string 建構函式丟擲 std::bad_alloc

[edit] 注意

  • 對於浮點型別,std::to_string 可能會產生意想不到的結果,因為返回字串中的有效數字可能為零,請參閱示例。
  • 返回值可能與 std::cout 預設列印的值顯著不同,請參閱示例。
  • std::to_string 依賴於當前的 C 語言環境進行格式化,因此,來自多個執行緒的併發 std::to_string 呼叫可能會導致部分序列化。
    • 整數型別過載的結果不依賴於當前的 C 語言環境,因此實現通常會避免在這些過載中訪問當前的 C 語言環境,以確保正確性和效能。然而,標準不保證這種避免。
(直到 C++26)

C++17 提供了 std::to_chars 作為更高效能的、獨立於語言環境的替代方案。

特性測試 標準 特性
__cpp_lib_to_string 202306L (C++26) 使用 std::format 重新定義 std::to_string

[edit] 示例

#include <cstdio>
#include <format>
#include <initializer_list>
#include <iostream>
#include <string>
 
#if __cpp_lib_to_string >= 202306L
constexpr auto revision() { return " (post C++26)"; }
#else
constexpr auto revision() { return " (pre C++26)"; }
#endif
 
int main()
{
    for (const double f : {1.23456789555555, 23.43, 1e-9, 1e40, 1e-40, 123456789.0})
    {
        std::cout << "to_string:\t" << std::to_string(f) << revision() << '\n';
 
        // Before C++26, the output of std::to_string matches std::printf.
        std::printf("printf:\t\t%f\n", f);
 
        // As of C++26, the output of std::to_string matches std::format.
        std::cout << std::format("format:\t\t{}\n", f);
 
        std::cout << "std::cout:\t" << f << "\n\n";
    }
}

可能的輸出

to_string:      1.234568 (pre C++26)
printf:         1.234568
format:         1.23456789555555
std::cout:      1.23457
 
to_string:      23.430000 (pre C++26)
printf:         23.430000
format:         23.43
std::cout:      23.43
 
to_string:      0.000000 (pre C++26)
printf:         0.000000
format:         1e-09
std::cout:      1e-09
 
to_string:      10000000000000000303786028427003666890752.000000 (pre C++26)
printf:         10000000000000000303786028427003666890752.000000
format:         1e+40
std::cout:      1e+40
 
to_string:      0.000000 (pre C++26)
printf:         0.000000
format:         1e-40
std::cout:      1e-40
 
to_string:      123456789.000000 (pre C++26)
printf:         123456789.000000
format:         123456789
std::cout:      1.23457e+08

[edit] 另請參閱

將整數或浮點值轉換為 wstring
(function) [編輯]
(C++11)(C++11)
將字串轉換為無符號整數
(function) [編輯]
(C++11)(C++11)(C++11)
將字串轉換為有符號整數
(function) [編輯]
(C++11)(C++11)(C++11)
將字串轉換為浮點值
(function) [編輯]
(C++17)
將整數或浮點值轉換為字元序列
(function) [編輯]