1. std::stoll
的基本介绍
std::stoll
(String to Long Long)是 C++ 标准库中的一个字符串转换函数,它用于将 std::string
或 std::wstring
转换为 long long
类型的整数。与 std::stol
相似,但 std::stoll
可以处理更大的整数范围。它同样支持不同进制的转换,如十进制、十六进制、八进制等。
1.1 std::stoll
的函数原型
long long stoll(const std::string& str, size_t* pos = nullptr, int base = 10);
long long stoll(const std::wstring& str, size_t* pos = nullptr, int base = 10);
1.2 参数解析
str
:要转换的字符串,必须是有效的整数格式。pos
(可选):如果提供pos
,stoll
会在*pos
中存储转换结束的位置(即最后一个转换的字符的下一个位置)。base
(可选):表示字符串所使用的进制,默认值为10
(十进制),支持2
到36
进制。
1.3 返回值
- 成功:返回转换后的
long long
类型整数。 - 失败:如果字符串不是有效的数字或超出了
long long
类型的范围,会抛出异常。
2. std::stoll
的实现原理
在 GCC 标准库的 <string>
头文件中,std::stoll
的底层实现基于 std::strtoll
,如下:
inline long long
stoll(const std::string& __str, size_t* __idx = 0, int __base = 10)
{
return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
__idx, __base);
}
2.1 解析实现代码
-
调用
std::strtoll
std::strtoll
(String to Long Long)是 C 语言中的标准库函数,它可以将 C 风格的字符串转换为long long
类型的整数,同时支持进制转换。 -
使用
__gnu_cxx::__stoa
进行封装
__stoa
是 GNU C++ 扩展库中的一个工具函数,它用于将字符串转换为long long
,并进行错误检查。 -
__str.c_str()
std::string
的.c_str()
方法返回一个const char*
,将std::string
转换为 C 风格字符串,以便std::strtoll
解析。
3. std::stoll
的实际用法
3.1 基础示例
#include <iostream>
#include <string>
int main() {
std::string str = "123456789123456789";
long long num = std::stoll(str); // 默认10进制
std::cout << "转换结果: " << num << std::endl;
return 0;
}
输出:
转换结果: 123456789123456789
3.2 使用 pos
参数记录转换结束的位置
#include <iostream>
#include <string>
int main() {
std::string str = "12345xyz";
std::size_t pos;
long long num = std::stoll(str, &pos);
std::cout << "转换结果: " << num << std::endl;
std::cout << "转换结束位置: " << pos << std::endl;
return 0;
}
输出:
转换结果: 12345
转换结束位置: 5
解析:
stoll
解析"12345"
后遇到"xyz"
,停止转换,并将pos
设为5
。
3.3 处理不同进制的转换
#include <iostream>
#include <string>
int main() {
std::string str = "1A"; // 16进制
long long num = std::stoll(str, nullptr, 16); // 以16进制解析
std::cout << "转换结果: " << num << std::endl;
return 0;
}
输出:
转换结果: 26
解析:
1A
在十六进制中等于26
,所以stoll
返回26
。
其他进制示例
std::stoll("1010", nullptr, 2); // 以二进制解析,返回 10
std::stoll("75", nullptr, 8); // 以八进制解析,返回 61
std::stoll("1F", nullptr, 16); // 以十六进制解析,返回 31
4. 处理异常情况
4.1 输入不是有效的数字
如果 str
不是有效的整数格式,std::stoll
会抛出 std::invalid_argument
异常:
#include <iostream>
#include <string>
#include <stdexcept>
int main() {
try {
std::string str = "abc";
long long num = std::stoll(str);
std::cout << "转换结果: " << num << std::endl;
} catch (const std::invalid_argument& e) {
std::cerr << "错误: 无效的数字字符串 -> " << e.what() << std::endl;
}
return 0;
}
输出:
错误: 无效的数字字符串 -> stol
4.2 数值超出 long long
范围
如果字符串表示的数值超出 long long
的范围,std::stoll
会抛出 std::out_of_range
异常:
#include <iostream>
#include <string>
#include <climits>
int main() {
try {
std::string str = "999999999999999999999";
long long num = std::stoll(str);
std::cout << "转换结果: " << num << std::endl;
} catch (const std::out_of_range& e) {
std::cerr << "错误: 数值超出范围 -> " << e.what() << std::endl;
}
return 0;
}
输出:
错误: 数值超出范围 -> stol
5. std::stoll
相关函数
函数 | 作用 | 返回类型 |
---|---|---|
std::stoi | 转换为 int | int |
std::stol | 转换为 long | long |
std::stoll | 转换为 long long | long long |
std::stoul | 转换为 unsigned long | unsigned long |
std::stoull | 转换为 unsigned long long | unsigned long long |
std::stof | 转换为 float | float |
std::stod | 转换为 double | double |
6. 结论
std::stoll
是std::strtoll
的封装,支持进制转换,并可转换为long long
类型,适用于更大的整数范围。- 提供
pos
参数记录转换位置。 - 可能抛出
std::invalid_argument
和std::out_of_range
异常。 - 若数值更大,使用
std::stoll
,若适用于long
类型的数值,则使用std::stol
。