std::stoi, std::stol, std::stoll
来自 cppreference.cn
< cpp | string | basic string
定义于头文件 <string> |
||
int stoi ( const std::string& str, std::size_t* pos = nullptr, int base = 10 ); |
(1) | (since C++11) |
int stoi ( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 ); |
(2) | (since C++11) |
long stol ( const std::string& str, std::size_t* pos = nullptr, int base = 10 ); |
(3) | (since C++11) |
long stol ( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 ); |
(4) | (since C++11) |
long long stoll( const std::string& str, std::size_t* pos = nullptr, int base = 10 ); |
(5) | (since C++11) |
long long stoll( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 ); |
(6) | (since C++11) |
解释字符串 str 中的有符号整数值。
设 ptr 为转换函数内部的 char* (1,3,5) 或 wchar_t* (2,4,6) 类型的指针。
1) 调用 std::strtol(str.c_str(), &ptr, base)。
2) 调用 std::wcstol(str.c_str(), &ptr, base)。
3) 调用 std::strtol(str.c_str(), &ptr, base)。
4) 调用 std::wcstol(str.c_str(), &ptr, base)。
5) 调用 std::strtoll(str.c_str(), &ptr, base)。
6) 调用 std::wcstoll(str.c_str(), &ptr, base)。
丢弃任何空白字符(通过调用 std::isspace 识别),直到找到第一个非空白字符,然后尽可能多地获取字符以形成有效的base-n(其中 n=base
)整数数字表示,并将它们转换为整数值。有效的整数值由以下部分组成
- (可选) 加号或减号
- (可选) 前缀 (
0
) 表示八进制基数(仅当基数为 8 或 0 时适用) - (可选) 前缀 (
0x
或0X
) 表示十六进制基数(仅当基数为 16 或 0 时适用) - 数字序列
base 的有效值集合为 {0, 2, 3, ..., 36}
。 base-2
整数的有效数字集合为 {0, 1}
,base-3
整数的有效数字集合为 {0, 1, 2}
,依此类推。 对于大于 10
的基数,有效数字包括字母字符,从 base-11
整数的 Aa
开始,到 base-36
整数的 Zz
。 字符的大小写被忽略。
当前安装的 C locale 可能会接受其他数字格式。
如果 base
的值为 0,则数字基数会自动检测:如果前缀为 0
,则基数为八进制,如果前缀为 0x
或 0X
,则基数为十六进制,否则基数为十进制。
如果输入序列中包含减号,则从数字序列计算出的数值将被取反,就像在结果类型中使用 一元减号 一样。
如果 pos 不是空指针,则 ptr 将接收 str.c_str() 中第一个未转换字符的地址,并且将计算该字符的索引并存储在 *pos 中,从而给出转换处理的字符数。
目录 |
[edit] 参数
str | - | 要转换的字符串 |
pos | - | 用于存储已处理字符数的整数地址 |
base | - | 数字基数 |
[edit] 返回值
对应于 str 内容的整数值。
[edit] 异常
- std::invalid_argument 如果无法执行转换。
- std::out_of_range 如果转换后的值超出结果类型的范围,或者如果底层函数 (std::strtol 或 errno 设置为 ERANGE。
[edit] 示例
运行此代码
#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 Calling with different radixes: 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
[edit] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 2009 | C++11 | std::out_of_range 不会被抛出,如果 std::strtol 或 errno 设置为 ERANGE |
将会抛出 |
[edit] 参见
(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 (函数) |