命名空间
变体
操作

std::to_string

来自 cppreference.cn
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
定义于头文件 <string>
std::string to_string( int value );
(1) (since C++11)
std::string to_string( long value );
(2) (since C++11)
std::string to_string( long long value );
(3) (since C++11)
std::string to_string( unsigned value );
(4) (since C++11)
std::string to_string( unsigned long value );
(5) (since C++11)
std::string to_string( unsigned long long value );
(6) (since C++11)
std::string to_string( float value );
(7) (since C++11)
std::string to_string( double value );
(8) (since C++11)
std::string to_string( long double value );
(9) (since 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)
(until C++26)
1-9) 将数值转换为字符串,如同使用 std::format("{}", value)
(since C++26)

目录

[编辑] 参数

value - 要转换的数值

[编辑] 返回值

一个包含转换值的字符串。

[编辑] 异常

可能从 std::bad_alloc std::string 构造函数抛出。

[编辑] 注意

  • 对于浮点类型,std::to_string 可能会产生意外的结果,因为返回的字符串中的有效位数可能为零,请参见示例。
  • 返回值可能与 std::cout 默认打印的结果显著不同,请参见示例。
  • std::to_string 依赖于当前的 C 区域设置来进行格式化,因此从多个线程并发调用 std::to_string 可能会导致调用的部分序列化。
    • 整数类型重载的结果不依赖于当前的 C 区域设置,因此,为了正确性和性能,实现通常避免在这些重载中访问当前的 C 区域设置。但是,标准不保证这种避免。
(until C++26)

C++17 提供了 std::to_chars 作为更高性能的、独立于区域设置的替代方案。

特性测试 Std 特性
__cpp_lib_to_string 202306L (C++26) 根据 std::format 重新定义 std::to_string

[编辑] 示例

#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

[编辑] 参见

将整数或浮点值转换为 wstring
(函数) [编辑]
(C++11)(C++11)
将字符串转换为无符号整数
(函数) [编辑]
(C++11)(C++11)(C++11)
将字符串转换为有符号整数
(函数) [编辑]
(C++11)(C++11)(C++11)
将字符串转换为浮点数值
(函数) [编辑]
(C++17)
将整数或浮点数值转换为字符序列
(函数) [编辑]