如何在 C++ 中将数字转换为字符串,反之亦然
因为这个问题每周都会被问到,所以这个FAQ可能会帮助很多用户.
Since this question gets asked about every week, this FAQ might help a lot of users.
如何在 C++ 中将整数转换为字符串
How to convert an integer to a string in C++
C++中如何将字符串转换为整数
how to convert a string into an integer in C++
如何在 C++ 中将浮点数转换为字符串
how to convert a floating-point number to a string in C++
如何在 C++ 中将字符串转换为浮点数
how to convert a string to a floating-point number in C++
推荐答案
C++11更新
从 C++11
标准开始,字符串到数字的转换(反之亦然)已内置到标准库中.以下所有函数都存在于 <string>
中(根据第 21.5 段).
Update for C++11
As of the C++11
standard, string-to-number conversion and vice-versa are built in into the standard library. All the following functions are present in <string>
(as per paragraph 21.5).
float stof(const string& str, size_t *idx = 0);
double stod(const string& str, size_t *idx = 0);
long double stold(const string& str, size_t *idx = 0);
int stoi(const string& str, size_t *idx = 0, int base = 10);
long stol(const string& str, size_t *idx = 0, int base = 10);
unsigned long stoul(const string& str, size_t *idx = 0, int base = 10);
long long stoll(const string& str, size_t *idx = 0, int base = 10);
unsigned long long stoull(const string& str, size_t *idx = 0, int base = 10);
每个都将字符串作为输入,并尝试将其转换为数字.如果无法构造有效数字,例如因为没有数字数据或数字超出类型的范围,则会引发异常(std::invalid_argument
或 std::out_of_range
).
Each of these take a string as input and will try to convert it to a number. If no valid number could be constructed, for example because there is no numeric data or the number is out-of-range for the type, an exception is thrown (std::invalid_argument
or std::out_of_range
).
如果转换成功且 idx
不是 0
,idx
将包含第一个未用于解码的字符的索引.这可能是最后一个字符后面的索引.
If conversion succeeded and idx
is not 0
, idx
will contain the index of the first character that was not used for decoding. This could be an index behind the last character.
最后,整数类型允许指定一个基数,对于大于 9 的数字,假定为字母表(a=10
直到 z=35
).您可以在此处找到更多关于可以为 浮点数解析的格式的信息,有符号整数 和 无符号整数.
Finally, the integral types allow to specify a base, for digits larger than 9, the alphabet is assumed (a=10
until z=35
). You can find more information about the exact formatting that can parsed here for floating-point numbers, signed integers and unsigned integers.
最后,对于每个函数还有一个重载,它接受 std::wstring
作为它的第一个参数.
Finally, for each function there is also an overload that accepts a std::wstring
as it's first parameter.
string to_string(int val);
string to_string(unsigned val);
string to_string(long val);
string to_string(unsigned long val);
string to_string(long long val);
string to_string(unsigned long long val);
string to_string(float val);
string to_string(double val);
string to_string(long double val);
这些更直接,您传递适当的数字类型并返回一个字符串.对于格式化选项,您应该返回 C++03 stringsream 选项并使用流操纵器,如此处的另一个答案中所述.
These are more straightforward, you pass the appropriate numeric type and you get a string back. For formatting options you should go back to the C++03 stringsream option and use stream manipulators, as explained in an other answer here.
如评论中所述,这些函数会退回到默认尾数精度,这可能不是最大精度.如果您的应用程序需要更高的精度,最好还是回到其他字符串格式化程序.
As noted in the comments these functions fall back to a default mantissa precision that is likely not the maximum precision. If more precision is required for your application it's also best to go back to other string formatting procedures.
还定义了类似的函数,命名为 to_wstring
,它们将返回 std::wstring
.
There are also similar functions defined that are named to_wstring
, these will return a std::wstring
.
相关文章