std::to_string
性病:至[医]弦
Defined in header | | |
---|---|---|
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
...
1%29将有符号的十进制整数转换为具有以下内容的字符串std::sprintf
(buf, "%d", value)
会产生足够大的buf
...
2%29将有符号的十进制整数转换为具有以下内容的字符串std::sprintf
(buf, "%ld", value)
会产生足够大的buf
...
3%29将有符号的十进制整数转换为具有以下内容的字符串std::sprintf
(buf, "%lld", value)
会产生足够大的buf
...
4%29将无符号十进制整数转换为内容与std::sprintf
(buf, "%u", value)
会产生足够大的buf
...
5%29将无符号十进制整数转换为内容与std::sprintf
(buf, "%lu", value)
会产生足够大的buf
...
6%29将无符号十进制整数转换为内容与std::sprintf
(buf, "%llu", value)
会产生足够大的buf
...
7,8%29将浮点值转换为具有与以下内容相同内容的字符串std::sprintf
(buf, "%f", value)
会产生足够大的buf
...
9%29将浮点值转换为与以下内容相同的字符串std::sprintf
(buf, "%Lf", value)
会产生足够大的buf
...
参数
value | - | a numeric value to convert |
---|
返回值
保存转换值的字符串。
注记
- 带浮点类型
std::to_string
可能会产生意外的结果,因为返回字符串中的有效位数可以为零,请参见示例。
- 返回值可能与
std::cout
默认情况下打印,请参见示例。
例
二次
#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 // Note: returns "0.000000"
std::string f_str3 = std::to_string(f3 // Note: Does not return "1e+40".
std::string f_str4 = std::to_string(f4 // Note: returns "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
二次
另见
to_wstring (C++11) | converts an integral or floating point value to wstring (function) |
---|---|
stoulstoull (C++11)(C++11) | converts a string to an unsigned integer (function) |
stoistolstoll (C++11)(C++11)(C++11) | converts a string to a signed integer (function) |
stofstodstold (C++11)(C++11)(C++11) | converts a string to a floating point value (function) |
to_chars (C++17) | converts an integer or floating-point value to a character sequence (function) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。