详解C++中stoi/stol/stoll函数的用法
在C++开发中,将字符串解析为整数是常见操作。C++11标准引入了`stoi`、`stol`和`stoll`函数,分别对应`int`、`long`和`long long`类型。本文深入解析这三个函数的签名、参数行为及不同进制下的转换逻辑,配合详细的代码示例,解决实际开发中的字符串转换需求。
stoi()函数
stoi函数用于将字符串转换为有符号的int整数。
函数签名与参数说明
头文件:#include
int stoi(const std::string& str, std::size_t* pos = 0, int base = 10);
主要参数:
str:需要转换的目标字符串。pos:一个指针,指向存储已消耗字符数量的变量。若传入非空指针,函数执行后会更新该值;默认为nullptr。base:进制基数。默认值为10。设为0时会自动检测进制(如以0x开头为十六进制,以0开头为八进制)。
第1步:指定转换字符串为十进制用法
目的:演示默认的十进制转换行为,观察前导空格忽略、字母截断及特殊前缀的处理。
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
int a;
size_t pos = 0;
string str;
// 常规整数转换
str = "-1235";
a = stoi(str);
cout << "a = " << a << endl; // 输出: -1235
str = "1235";
a = stoi(str);
cout << "a = " << a << endl; // 输出: 1235
// 跳过空白符:遇到非数字字符停止
str = " -12 35";
a = stoi(str, &pos);
cout << "a = " << a << endl; // 输出: -12 (只读取了 "-12")
cout << "pos = " << pos << endl; // 输出: 5 ("-12"后的空格索引)
// 字母提前终止
str = " -12ab35";
a = stoi(str, &pos);
cout << "a = " << a << endl; // 输出: -12
cout << "pos = " << pos << endl; // 输出: 5
// 注意:默认base=10时,不识别八进制前缀0和十六进制前缀0x
str = "0123";
a = stoi(str);
cout << "a = " << a << endl; // 输出: 123 (按十进制解读)
str = "0x123";
a = stoi(str);
cout << "a = " << a << endl; // 输出: 0 (遇到'x'停止,返回起始部分,即0)
return 0;
}
第2步:指定转换字符串为十六进制用法
目的:演示显式设置base=16以及自动检测模式base=0的区别。
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
int a;
size_t pos = 0;
string str;
// base=16:必须提供合法的十六进制字符
str = "0x123";
a = stoi(str, NULL, 16);
cout << "a = " << a << endl; // 输出: 291 (0x123 = 1*256 + 2*16 + 3)
// base=0:自动检测,识别0x前缀并当作十六进制
str = "0x123";
a = stoi(str, NULL, 0);
cout << "a = " << a << endl; // 输出: 291
// 负号处理
str = "-12";
a = stoi(str, &pos, 16); // -(2 + 1*16) = -18
cout << "a = " << a << endl; // 输出: -18
cout << "pos = " << pos << endl; // 输出: 3
// 大写字母也支持
str = "12";
a = stoi(str, &pos, 16); // 18
cout << "a = " << a << endl; // 输出: 18
cout << "pos = " << pos << endl; // 输出: 2
// 十六进制下不区分大小写
str = " -ab";
a = stoi(str, &pos, 16); // -(11 + 10*16) = -171
cout << "a = " << a << endl; // 输出: -171
cout << "pos = " << pos << endl; // 输出: 5
// 纯数字在十六进制下依然有效
str = "0123";
a = stoi(str, NULL, 16); // 3 + 2*16 + 1*256 = 291
cout << "a = " << a << endl; // 输出: 291
return 0;
}
第3步:指定转换字符串为八进制用法
目的:展示八进制转换时的行为,特别注意当输入包含无效八进制字符(如8、9、a)时的截断结果。
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
int a;
size_t pos = 0;
string str;
// 显式指定八进制
str = "0x123";
a = stoi(str, NULL, 8);
cout << "a = " << a << endl; // 输出: 0 ('x'不是合法八进制字符,立即停止)
// base=0:自动检测0开头,识别为八进制
str = "0123";
a = stoi(str, NULL, 0);
cout << "a = " << a << endl; // 输出: 83 (3 + 2*8 + 1*64)
// 负数转换
str = "-12";
a = stoi(str, &pos, 8); // -(2 + 1*8) = -10
cout << "a = " << a << endl; // 输出: -10
cout << "pos = " << pos << endl; // 输出: 3
// 混合内容截断
str = " -12 35";
a = stoi(str, &pos, 8); // 截取到"-12"
cout << "a = " << a << endl; // 输出: -10
cout << "pos = " << pos << endl; // 输出: 5
}
stol()函数
stol函数功能与stoi完全一致,唯一的区别在于返回值类型为有符号 long整数,适合处理更大的整型数据范围。
第1步:指定转换字符串为十进制用法
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
long a;
size_t pos = 0;
string str;
str = "-1235";
a = stol(str);
cout << "a = " << a << endl; // 输出: -1235
str = "1235";
a = stol(str);
cout << "a = " << a << endl; // 输出: 1235
// 空白符跳过与截断测试
str = " -12 35";
a = stol(str, &pos);
cout << "a = " << a << endl; // 输出: -12
cout << "pos = " << pos << endl; // 输出: 5
str = " -12ab35";
a = stol(str, &pos);
cout << "a = " << a << endl; // 输出: -12
cout << "pos = " << pos << endl; // 输出: 5
// 无前缀时的十进制解读
str = "0123";
a = stol(str);
cout << "a = " << a << endl; // 输出: 123
}
第2步:指定转换字符串为十六进制用法
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
long a;
size_t pos = 0;
string str;
// base=16 明确指定
str = "0x123";
a = stol(str, NULL, 16);
cout << "a = " << a << endl; // 输出: 291
// base=0 自动识别
str = "0x123";
a = stol(str, NULL, 0);
cout << "a = " << a << endl; // 输出: 291
// 负号与截断
str = "-12";
a = stol(str, &pos, 16);
cout << "a = " << a << endl; // 输出: -18
cout << "pos = " << pos << endl; // 输出: 3
str = "12";
a = stol(str, &pos, 16);
cout << "a = " << a << endl; // 输出: 18
cout << "pos = " << pos << endl; // 输出: 2
// 大写字母解析
str = " -ab";
a = stol(str, &pos, 16);
cout << "a = " << a << endl; // 输出: -171
cout << "pos = " << pos << endl; // 输出: 5
}
第3步:指定转换字符串为八进制用法
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
long a;
size_t pos = 0;
string str;
// 非法字符导致无法解析
str = "0x123";
a = stol(str, NULL, 8);
cout << "a = " << a << endl; // 输出: 0
// base=0 触发自动八进制识别
str = "0123";
a = stol(str, NULL, 0);
cout << "a = " << a << endl; // 输出: 83
// 基础计算:-(2 + 1*8)
str = "-12";
a = stol(str, &pos, 8);
cout << "a = " << a << endl; // 输出: -10
cout << "pos = " << pos << endl; // 输出: 3
str = "12";
a = stol(str, &pos, 8);
cout << "a = " << a << endl; // 输出: 10
cout << "pos = " << pos << endl; // 输出: 2
}
stoll()函数
stoll函数同样功能相同,但返回值类型为有符号 long long,适用于更大范围的整数转换。
第1步:指定转换字符串为十进制用法
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
long long a;
size_t pos = 0;
string str;
str = "-1235";
a = stoll(str);
cout << "a = " << a << endl; // 输出: -1235
str = "1235";
a = stoll(str);
cout << "a = " << a << endl; // 输出: 1235
// 截断行为保持一致
str = " -12 35";
a = stoll(str, &pos);
cout << "a = " << a << endl; // 输出: -12
cout << "pos = " << pos << endl; // 输出: 5
str = " -12ab35";
a = stoll(str, &pos);
cout << "a = " << a << endl; // 输出: -12
cout << "pos = " << pos << endl; // 输出: 5
str = "0123";
a = stoll(str);
cout << "a = " << a << endl; // 输出: 123
str = "0x123";
a = stoll(str);
cout << "a = " << a << endl; // 输出: 0 (默认十进制,遇到'x'停止)
}
第2步:指定转换字符串为十六进制用法
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
long long a;
size_t pos = 0;
string str;
// 显式十六进制
str = "0x123";
a = stoll(str, NULL, 16);
cout << "a = " << a << endl; // 输出: 291
// 自动检测
str = "0x123";
a = stoll(str, NULL, 0);
cout << "a = " << a << endl; // 输出: 291
// 负号处理
str = "-12";
a = stoll(str, &pos, 16);
cout << "a = " << a << endl; // 输出: -18
cout << "pos = " << pos << endl; // 输出: 3
str = "12";
a = stoll(str, &pos, 16);
cout << "a = " << a << endl; // 输出: 18
cout << "pos = " << pos << endl; // 输出: 2
str = " -12 35";
a = stoll(str, &pos, 16);
cout << "a = " << a << endl; // 输出: -18
cout << "pos = " << pos << endl; // 输出: 5
// 字母混合
str = " -ab";
a = stoll(str, &pos, 16);
cout << "a = " << a << endl; // 输出: -171
cout << "pos = " << pos << endl; // 输出: 5
}
第3步:指定转换字符串为八进制用法
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
long long a;
size_t pos = 0;
string str;
// 无效前缀
str = "0x123";
a = stoll(str, NULL, 8);
cout << "a = " << a << endl; // 输出: 0
// base=0 自动识别
str = "0123";
a = stoll(str, NULL, 0);
cout << "a = " << a << endl; // 输出: 83
// 负数运算:-(2 + 1*8)
str = "-12";
a = stoll(str, &pos, 8);
cout << "a = " << a << endl; // 输出: -10
cout << "pos = " << pos << endl; // 输出: 3
str = "12";
a = stoll(str, &pos, 8);
cout << "a = " << a << endl; // 输出: 10
cout << "pos = " << pos << endl; // 输出: 2
}
注意:以上函数均属于 C++11 标准引入的特性。在使用 GCC 编译器进行编译时,务必添加 -std=c++11 参数以启用相关支持。
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













