比较二进制数字输入的字符时,抛出'std :: out_of_range实例后调用终止

Pulkit米塔尔

我是编码新手,遇到以下错误。

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::at: __n (which is 6) >= this->size() (which is 6)
Aborted (core dumped)

以下代码是:

#include <iostream>
#include<algorithm>
#include<string>
using namespace std;

int main()
{
    int a,b;
    cin>>a>>b;

    string  sa=to_string(a);
    string sb=to_string(b);


    int l=sa.length();

    for(int i=0;i<l;i++)
    {
        if(sa.at(i)==sb.at(i))
        {
            cout<<0;
        }
        else
            cout<<1;
    }
}

这个问题的输入是

1010100

0100101

任何帮助,将不胜感激!

云诺什

由于前导零,因此读取第二个输入100101。
尝试访问尽可能多的1010100字符将超出其长度。

为了解决这两个读入作为字符串。

例如,如下所示(请注意,该代码仅演示了我建议的更改,它仍然容易受到不同的长输入之类的影响,100 10并具有其他缺点):

    string  sa,sb;
    cin>>sa>>sb;
    /* no to_string() */

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

抛出'std :: out_of_range'实例后调用终止

抛出'std :: out_of_range'what():vector :: _ M_range_check实例后终止调用

c++ 在抛出'std::out_of_range' std::vector 的实例后调用终止

在C ++中抛出'std :: out_of_range'实例后调用终止

在抛出 'std::out_of_range' what() 实例后调用终止:basic_string::at: __n 错误

将字母向前移动 3 个字母的程序,错误:在抛出 'std::out_of_range' 实例后调用终止

"if (argc < 2 || argc > 2)" 应该有 2 个参数吗?& 在抛出“std::out_of_range”错误实例后调用终止

为什么“在抛出一个 'std::out_of_range' what(): basic_string::at: __n (which is 0) >= this->size() (which is 0) 实例后终止调用?

C++ 捕获错误“抛出'std::out_of_range'的实例”

在抛出 std::exception 实例后调用终止

抛出'std :: regex_error'实例后调用终止

在抛出“std::system_error”实例后调用 Tensorflow 终止

std :: stoul不会抛出std :: out_of_range

使用gcc编译代码后,不会输出std :: out_of_range异常字符串

std::stoi() 错误——“在抛出 'std::invalid_argument' 实例后终止调用”

C++ 抛出错误“在抛出‘std::bad_alloc’的实例后终止调用”

得到错误“抛出'std :: bad_alloc'what():whatstd :: bad_alloc实例后终止调用”

抛出“std::system_error”线程池实例后调用终止

为什么在抛出'std :: bad_alloc'实例后终止调用?

C ++错误:抛出'std :: bad_alloc'实例后终止调用

Tensorflow MNIST:抛出'std :: bad_alloc'实例后调用终止

抛出“std::invalid_argument”实例后调用终止 what(): leetcode 问题中的 stoi 错误

在抛出'std::runtime_error' what() 实例后调用终止:filebuf 和 ostream 的 I/O 错误

在抛出“std::regex_error”what() 实例后调用终止:括号未关闭

抛出'std::bad_alloc'的实例后调用C++终止

C ++中带有矢量的'std :: out_of_range'实例

以类型为std :: out_of_range的未捕获异常终止终止错误

std :: stod抛出应为有效字符串的out_of_range错误

出现错误:抛出'std :: bad :: alloc'what():what std :: bad_alloc实例后终止终止