std:: stoi, std:: stol, std:: stoll
|
定义于头文件
<string>
|
||
|
int
stoi
(
const
std::
string
&
str,
std:: size_t * pos = nullptr, int base = 10 ) ; |
(1) | (C++11 起) |
|
int
stoi
(
const
std::
wstring
&
str,
std:: size_t * pos = nullptr, int base = 10 ) ; |
(2) | (C++11 起) |
|
long
stol
(
const
std::
string
&
str,
std:: size_t * pos = nullptr, int base = 10 ) ; |
(3) | (C++11 起) |
|
long
stol
(
const
std::
wstring
&
str,
std:: size_t * pos = nullptr, int base = 10 ) ; |
(4) | (C++11 起) |
|
long
long
stoll
(
const
std::
string
&
str,
std:: size_t * pos = nullptr, int base = 10 ) ; |
(5) | (C++11 起) |
|
long
long
stoll
(
const
std::
wstring
&
str,
std:: size_t * pos = nullptr, int base = 10 ) ; |
(6) | (C++11 起) |
将字符串 str 中的内容解析为有符号整数值。
令 ptr 作为转换函数内部的指针,其类型为 char * (1,3,5) 或 wchar_t * (2,4,6) (根据具体情况而定)。
丢弃所有空白字符(通过调用
std::isspace
识别),直到找到第一个非空白字符,然后尽可能多地提取字符以形成有效的
base-n
(其中n=
base
)整数表示形式,并将其转换为整数值。有效的整数值由以下部分组成:
- (可选) 正负号
-
(可选)
表示八进制基数的前缀 (
0)(仅当基数为 8 或 0 时适用) -
(可选)
表示十六进制基数的前缀 (
0x或0X)(仅当基数为 16 或 0 时适用) - 数字序列
基数的有效取值范围为
{0, 2, 3, ..., 36}
。对于基数为
2
的整数,有效数字集合为
{0, 1}
;对于基数为
3
的整数,有效数字集合为
{0, 1, 2}
,依此类推。对于大于
10
的基数,有效数字包含字母字符:从基数为
11
的整数对应的
Aa
开始,直至基数为
36
的整数对应的
Zz
。字符的大小写将被忽略。
当前安装的 C locale 可能接受其他数字格式。
若
base
的值为
0
,将自动检测数字进制:若前缀为
0
则为八进制,若前缀为
0x
或
0X
则为十六进制,否则为十进制。
如果减号是输入序列的一部分,则根据数字序列计算出的数值将被取反,如同在结果类型中通过 一元减号运算符 处理。
如果 pos 不是空指针,那么 ptr 将接收到 str. c_str ( ) 中第一个未转换字符的地址,该字符的索引将被计算并存储在 * pos 中,表示转换处理的字符数量。
目录 |
参数
| str | - | 要转换的字符串 |
| pos | - | 用于存储已处理字符数的整数地址 |
| base | - | 数值基数 |
返回值
与 str 内容对应的整数值。
异常
- 如果无法执行转换,则抛出 std::invalid_argument 。
- 如果转换后的值超出结果类型的范围,或底层函数( std::strtol 或 std::strtoll )将 errno 设置为 ERANGE ,则抛出 std::out_of_range 。
示例
#include <iomanip> #include <iostream> #include <stdexcept> #include <string> #include <utility> int main() { const auto data = { "45", "+45", " -45", "3.14159", "31337 with words", "words and 2", "12345678901", }; for (const std::string s : data) { std::size_t pos{}; try { std::cout << "std::stoi(" << std::quoted(s) << "): "; const int i{std::stoi(s, &pos)}; std::cout << i << "; pos: " << pos << '\n'; } catch (std::invalid_argument const& ex) { std::cout << "std::invalid_argument::what(): " << ex.what() << '\n'; } catch (std::out_of_range const& ex) { std::cout << "std::out_of_range::what(): " << ex.what() << '\n'; const long long ll{std::stoll(s, &pos)}; std::cout << "std::stoll(" << std::quoted(s) << "): " << ll << "; pos: " << pos << '\n'; } } std::cout << "\nCalling with different radixes:\n"; for (const auto& [s, base] : {std::pair<const char*, int> {"11", 2}, {"22", 3}, {"33", 4}, {"77", 8}, {"99", 10}, {"FF", 16}, {"jJ", 20}, {"Zz", 36}}) { const int i{std::stoi(s, nullptr, base)}; std::cout << "std::stoi(" << std::quoted(s) << ", nullptr, " << base << "): " << i << '\n'; } }
可能的输出:
std::stoi("45"): 45; pos: 2
std::stoi("+45"): 45; pos: 3
std::stoi(" -45"): -45; pos: 4
std::stoi("3.14159"): 3; pos: 1
std::stoi("31337 with words"): 31337; pos: 5
std::stoi("words and 2"): std::invalid_argument::what(): stoi
std::stoi("12345678901"): std::out_of_range::what(): stoi
std::stoll("12345678901"): 12345678901; pos: 11
使用不同基数调用:
std::stoi("11", nullptr, 2): 3
std::stoi("22", nullptr, 3): 8
std::stoi("33", nullptr, 4): 15
std::stoi("77", nullptr, 8): 63
std::stoi("99", nullptr, 10): 99
std::stoi("FF", nullptr, 16): 255
std::stoi("jJ", nullptr, 20): 399
std::stoi("Zz", nullptr, 36): 1295
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 适用范围 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| LWG 2009 | C++11 |
当
std::out_of_range 不会抛出,如果 std::strtol 或 std::strtoll 将 errno 设置为 ERANGE |
将会抛出 |
参见
|
(C++11)
(C++11)
|
将字符串转换为无符号整数
(函数) |
|
(C++11)
(C++11)
(C++11)
|
将字符串转换为浮点值
(函数) |
|
(C++11)
|
将字节字符串转换为整数值
(函数) |
|
(C++11)
|
将字节字符串转换为无符号整数值
(函数) |
|
(C++11)
(C++11)
|
将字节字符串转换为
std::intmax_t
或
std::uintmax_t
(函数) |
|
(C++17)
|
将字符序列转换为整数或浮点值
(函数) |
|
(C++11)
|
将字节字符串转换为整数值
(函数) |
|
(C++11)
|
将整型或浮点值转换为
string
(函数) |
|
(C++11)
|
将整型或浮点值转换为
wstring
(函数) |