如何在C ++中将数字转换为字符串,反之亦然 [英] How to convert a number to string and vice versa in C++
问题描述
由于这个问题每周都会出现问题,因此常见问题可能会帮助很多用户。
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
)。您可以在此处找到有关可以解析浮点数, a href =http://en.cppreference.com/w/cpp/string/basic_string/stol>有符号整数和无符号整数。
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
.
这篇关于如何在C ++中将数字转换为字符串,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!