且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C ++中将数字转换为字符串,反之亦然

更新时间:2023-02-19 12:27:32

更新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.