如何在C++中将数字转换为字符串,反之亦然 [英] How to convert a number to string and vice versa in C++

查看:44
本文介绍了如何在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 标准开始,字符串到数字的转换(反之亦然)已内置到标准库中. 中包含以下所有函数(根据第 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_argumentstd::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 不是0idx 将包含未用于解码的第一个字符的索引.这可能是最后一个字符后面的索引.

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.

这篇关于如何在C++中将数字转换为字符串,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆