std::stoi, std::stol, std::stoll
来自 zh.cppreference.net
| 定义于头文件 <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) 的指针。
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 识别),直到找到第一个非空白字符,然后取尽可能多的字符构成一个合法的以 **_n 为基_ (其中 n=) 的整数数字表示,并将其转换为整数值。合法的 整数值包含以下部分:
base
- (可选) 正号或负号
- (可选) 前缀 (
0),指示八进制基(仅在基为8或0时适用) - (可选) 前缀 (
0x或0X),指示十六进制基(仅在基为16或0时适用) - 数字序列
基的有效值集合为 {0, 2, 3, ..., 36}。以 _n_ 为基的整数的有效数字集合为 2,以 _n_ 为基的整数的有效数字集合为 {0, 1},以此类推。对于大于 3 的基,有效数字包括字母字符,从 {0, 1, 2} 开始的字母对应以 _n_ 为基的整数,到 10 对应以 _n_ 为基的整数。字母的大小写被忽略。
Aa当前安装的 C 11本地环境Zz 可能接受额外的数字格式。
36如果
的值为 ,则自动检测数字基:如果前缀为 ,则基为八进制;如果前缀为
或 ,则基为十六进制;否则基为十进制。
base0如果输入序列中包含负号,则根据数字序列计算出的数值将在结果类型中通过0一元负号0x 取反。
0X如果
不是空指针,则 将接收 中第一个未转换字符的地址,并计算该字符的索引存储在
中,从而给出转换过程中已处理的字符数。
posptrstr.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
(函数) |