std::stoi, std::stol, std::stoll
来自cppreference.com
< cpp | string | basic string
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <string> 中定义
|
||
int stoi( const std::string& str, size_t *pos = 0, int base = 10 ); |
(1) | (C++11 起) |
long stol( const std::string& str, size_t *pos = 0, int base = 10 ); |
(2) | (C++11 起) |
long long stoll( const std::string& str, size_t *pos = 0, int base = 10 ); |
(3) | (C++11 起) |
解释字符串中的有符号整数
str
.原文:
Interprets a signed integer value in the string
str
.Function discards any whitespace characters until first non-whitespace character is found. Then it takes as many characters as possible to form a valid base-n (where n=base) integer number representation and converts them to an integer value. The valid integer value consists of the following parts:
- (可选) plus or minus sign
- (可选) prefix (
0
) indicating octal base (applies only when the base is 8) - (可选) prefix (
0x
or0X
) indicating hexadecimal base (applies only when the base is 16) - a sequence of digits
The set of valid digits for base-2 integer is 01
, for base-3 integer is 012
, and so on. For bases larger than 10
, valid digits include alphabetic characters, starting from Aa
for base-11 integer, to Zz
for base-36 integer. The case of the characters is ignored.
的第一个未转化的字符的索引被存储在
pos
。 NULL通过为pos
,被忽视原文:
The index of the first unconverted character is stored in
pos
. If NULL is passed as pos
, it is ignored.目录 |
[编辑] 参数
str | - | 要转换的字符串。一个整数来存储索引的第一个未转换的字符
|
pos | - | 原文: address of an integer to store the index of the first unconverted character |
base | - | 地址。基数
|
[编辑] 返回值
将字符串转换为指定的有符号整数类型.
原文:
The string converted to the specified signed integer type.
[编辑] 例外
- std::invalid_argument如果没有可以进行转换原文:std::invalid_argument if no conversion could be performed
- std::out_of_range如果转换后的值会掉出来的结果类型的范围.原文:std::out_of_range if the converted value would fall out of the range of the result type.
[编辑] 示例
#include <iostream> #include <string> int main() { std::string test = "45"; int myint = stoi(test); std::cout << myint << '\n'; }
输出:
45
[编辑] 另请参阅
一个字节的字符串转换为整数的值 原文: converts a byte string to an integer value (函数) | |
(C++11) (C++11) |
将字符串转换为无符号整数 原文: converts a string to an unsigned integer (函数) |
(C++11) (C++11) (C++11) |
将字符串转换为浮点值 原文: converts a string to an floating point value (函数) |
(C++11) |
到 string 转换为整数或浮点值 原文: converts an integral or floating point value to string (函数) |