C++ 参考手册
- C++11
- C++14
- C++17
- C++20
- C++ 编译器支持情况表
- 独立与宿主实现
- C++ 语言
- C++ 关键词
- 预处理器
- C++ 标准库头文件
- 具名要求
- 功能特性测试 (C++20)
- 工具库
- 类型支持(基本类型、RTTI、类型特性)
- 概念库 (C++20)
- 错误处理
- 动态内存管理
- 日期和时间工具
- 字符串库
- std::basic_string
- std::basic_string<CharT,Traits,Allocator>::npos
- std::basic_string<CharT,Traits,Allocator>::at
- std::hash (std::string, std::wstring, std::u16string, std::u32string, std::pmr::string, std::pmr::wstring, std::pmr::u16string, std::pmr::u32string)
- std::basic_string<CharT,Traits,Allocator>::assign
- std::basic_string<CharT,Traits,Allocator>::get_allocator
- std::basic_string<CharT,Traits,Allocator>::substr
- std::basic_string<CharT,Traits,Allocator>::basic_string
- std::basic_string<CharT,Traits,Allocator>::operator=
- std::basic_string<CharT,Traits,Allocator>::operator[]
- std::basic_string<CharT,Traits,Allocator>::front
- std::basic_string<CharT,Traits,Allocator>::back
- std::basic_string<CharT,Traits,Allocator>::data
- std::basic_string<CharT,Traits,Allocator>::c_str
- std::basic_string<CharT,Traits,Allocator>::operator basic_string_view
- std::basic_string<CharT,Traits,Allocator>::begin, std::basic_string<CharT,Traits,Allocator>::cbegin
- std::basic_string<CharT,Traits,Allocator>::end, std::basic_string<CharT,Traits,Allocator>::cend
- std::basic_string<CharT,Traits,Allocator>::rbegin, std::basic_string<CharT,Traits,Allocator>::crbegin
- std::basic_string<CharT,Traits,Allocator>::rend, std::basic_string<CharT,Traits,Allocator>::crend
- std::basic_string<CharT,Traits,Allocator>::empty
- std::basic_string<CharT,Traits,Allocator>::size, std::basic_string<CharT,Traits,Allocator>::length
- std::basic_string<CharT,Traits,Allocator>::max_size
- std::basic_string<CharT,Traits,Allocator>::reserve
- std::basic_string<CharT,Traits,Allocator>::capacity
- std::basic_string<CharT,Traits,Allocator>::shrink_to_fit
- std::basic_string<CharT,Traits,Allocator>::clear
- std::basic_string<CharT,Traits,Allocator>::insert
- std::basic_string<CharT,Traits,Allocator>::erase
- std::basic_string<CharT,Traits,Allocator>::push_back
- std::basic_string<CharT,Traits,Allocator>::pop_back
- std::basic_string<CharT,Traits,Allocator>::append
- std::basic_string<CharT,Traits,Allocator>::operator+=
- std::basic_string<CharT,Traits,Allocator>::compare
- std::basic_string<CharT,Traits,Allocator>::starts_with
- std::basic_string<CharT,Traits,Allocator>::ends_with
- std::basic_string<CharT,Traits,Allocator>::replace
- std::basic_string<CharT,Traits,Allocator>::copy
- std::basic_string<CharT,Traits,Allocator>::resize
- std::basic_string<CharT,Traits,Allocator>::swap
- std::basic_string<CharT,Traits,Allocator>::find
- std::basic_string<CharT,Traits,Allocator>::rfind
- std::basic_string<CharT,Traits,Allocator>::find_first_of
- std::basic_string<CharT,Traits,Allocator>::find_first_not_of
- std::basic_string<CharT,Traits,Allocator>::find_last_of
- std::basic_string<CharT,Traits,Allocator>::find_last_not_of
- std::operator+(std::basic_string)
- std::swap(std::basic_string)
- std::literals::string_literals::operator""s
- std::erase, std::erase_if (std::basic_string)
- operator<<,>>(std::basic_string)
- std::getline
- operator==,!=,<,<=,>,>=,<=>(std::basic_string)
- std::stoi, std::stol, std::stoll
- std::stoul, std::stoull
- std::stof, std::stod, std::stold
- std::to_string
- std::to_wstring
- std::basic_string 的推导指引
- std::basic_string_view
- 空终止字节字符串
- 空终止多字节字符串
- 空终止宽字符串
- std::char_traits
- 注释
- 容器库
- 迭代器库
- 范围库 (C++20)
- 算法库
- 数值库
- 输入/输出库
- 文件系统库
- 本地化库
- 正则表达式库
- 原子操作库
- 线程支持库
- 实验性 C++ 特性
- 有用的资源
- 索引
- std 符号索引
- 协程支持 (C++20)
- C++ 关键词
位置:首页 > C++ 参考手册 >字符串库 >std::basic_string > std::to_string
std::to_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 起) |
参数
value | - | 需要转换的数值 |
返回值
一个包含转换后值的字符串
异常
可能从 std::string 的构造函数抛出 std::bad_alloc 。
注意
- 对于浮点类型,
std::to_string
可能产生不期待的结果,因为返回的字符串中的有效位数能为零,见示例。 - 返回值可以明显地有别于
std::cout
所默认打印的结果,见示例。 -
std::to_string
由于格式化目的依赖本地环境,从而从多个线程同时调用std::to_string
可能会导致调用的部分序列化结果。 C++17 提供高性能、不依赖本地环境的替用品 std::to_chars 。
示例
运行此代码
#include <iostream> #include <string> int main() { double f = 23.43; double f2 = 1e-9; double f3 = 1e40; double f4 = 1e-40; double f5 = 123456789; std::string f_str = std::to_string(f); std::string f_str2 = std::to_string(f2); // 注意:返回 "0.000000" std::string f_str3 = std::to_string(f3); // 注意:不返回 "1e+40". std::string f_str4 = std::to_string(f4); // 注意:返回 "0.000000" std::string f_str5 = std::to_string(f5); std::cout << "std::cout: " << f << '\n' << "to_string: " << f_str << "\n\n" << "std::cout: " << f2 << '\n' << "to_string: " << f_str2 << "\n\n" << "std::cout: " << f3 << '\n' << "to_string: " << f_str3 << "\n\n" << "std::cout: " << f4 << '\n' << "to_string: " << f_str4 << "\n\n" << "std::cout: " << f5 << '\n' << "to_string: " << f_str5 << '\n'; }
输出:
std::cout: 23.43 to_string: 23.430000 std::cout: 1e-09 to_string: 0.000000 std::cout: 1e+40 to_string: 10000000000000000303786028427003666890752.000000 std::cout: 1e-40 to_string: 0.000000 std::cout: 1.23457e+08 to_string: 123456789.000000
参阅
(C++11) |
转换整数或浮点值为 wstring (函数) |
(C++11)(C++11) |
转换字符串为无符号整数 (函数) |
(C++11)(C++11)(C++11) |
转换字符串为有符号整数 (函数) |
(C++11)(C++11)(C++11) |
转换字符串为浮点值 (函数) |
(C++17) |
转换整数或浮点值到字符序列 (函数) |