SIGABRT:使用斯托伊/斯托尔时超出范围
SIGABRT:out of range when using stoi/stol
//我正在尝试从HackerEarth
:二进制查询中解决此问题。
虽然最初,这个问题对我来说听起来很容易,但是当实际提交我的代码以在所有测试用例上运行时,我的代码抛出了SIGABRT error
。
检查错误后,我发现错误类型为out of range
。我无法弄清楚如何解决这个问题:问题:https://www.hackerearth.com/practice/data-structures/arrays/1-d/practice-problems/algorithm/range-query-2/
#include<iostream>
#include<algorithm>
#include<cctype>
using namespace std;
int main()
{
long N,Q,L,R,X;
int ch=0,buf;
unsigned long long intrim;
string str,cstr;
scanf("%ld %ld",&N,&Q);
cin.ignore();
getline(cin,str);
str.erase((remove_if(str.begin(),str.end(),(int(*)(int))isspace)),str.end()); // here its a key point
//cout<<str;
for(int i=0;i<Q;i++)
{
scanf("%d",&ch);
if(ch==1) // alter the X bit
{
scanf("%ld",&X);
if(str[X-1]==0){
str[X-1]=1;
}
else {
str[X-1]=0;
}
}
else if(ch==0)
{
scanf("%ld %ld",&L,&R);
cstr.append((str.begin()+L-1),str.begin()+R);
intrim=std::stoull(cstr,nullptr,2);
if(intrim%2==0){
cout<<"EVEN"<<endl;
}
else{
cout<<"ODD"<<endl;
}
cstr.clear();
}
}
}
stoi 返回一个整数
STOL 返回一个长整数
斯托尔返回一个长整型整数
因此,当您将字符串转换为数字时,请考虑可能的范围并相应地使用。
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 尝试通过多个向量访问变量时,向量下标超出范围
- 错误:未在此范围内声明'reverse'
- 正在将指针转换为范围
- 使用std::transform将一个范围的元素添加到另一个范围中
- 在基于范围的for循环中使用结构化绑定声明
- 如何计算数据类型的范围,例如int
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 在C++中查找范围的长度
- 如何设置一个范围来提取我想要获得的信息
- 并行用于C++17中数组索引范围内的循环
- 为左值和右值的包装器实现C++范围
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 超出范围时使用对象
- 不计算一个范围内的完美数
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- SIGABRT:使用斯托伊/斯托尔时超出范围
- C++范围基于for环路提供SIGABRT,而正常环路工作正常