Namespaces
Variants

std::to_string

来自 zh.cppreference.net
 
 
 
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 起)

参数

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 作为更高性能的本地化无关替代方案。

功能测试 宏 值 标准 功能特性
__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';
        // C++26之前,std::to_string的输出与std::printf匹配
        std::printf("printf:\t\t%f\n", f);
        // 从C++26开始,std::to_string的输出与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

参见

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