谁能解释我在输出中遇到的错误以及如何删除它?

发布于 2025-02-11 07:01:14 字数 1311 浏览 1 评论 0原文

我的代码:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int BinaryToDecimal(int n)
{
    int ans = 0;
    int x = 1;

    while (n > 0)
    {
        int y = n % 10;
        ans = ans + x * y;
        x = x * 2;
        n = n / 10;
    }
    return ans;
}

int DecimalToBinary(int num)
{
    vector<int> vect;

    while (num > 0)
    {
        vect.push_back(num % 2);
        num = num / 2;
    }
    int s = vect.size();
    int i = s - 1;
    for (i = s - 1; i >= 0; i--)
    {
        cout << vect.at(i);
    }
    return vect.at(i);
}

int main()
{
    int a, b;
    cout << "Enter first number: " << endl;
    cin >> a;
    cout << "Enter second number: " << endl;
    cin >> b;

    int a_deci = BinaryToDecimal(a);
    int b_deci = BinaryToDecimal(b);

    int sum = a_deci + b_deci;

    DecimalToBinary(sum);
    cout << endl;
    return 0;
}    

输出:

Enter first number: 
10101
Enter second number:
11010
101111terminate called after throwing an instance of 'std::out_of_range'what():  vector::_M_range_check: __n (which is 18446744073709551615) >= this->size() (which is 6)

此错误消息是什么意思,我该如何修复?

My code:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int BinaryToDecimal(int n)
{
    int ans = 0;
    int x = 1;

    while (n > 0)
    {
        int y = n % 10;
        ans = ans + x * y;
        x = x * 2;
        n = n / 10;
    }
    return ans;
}

int DecimalToBinary(int num)
{
    vector<int> vect;

    while (num > 0)
    {
        vect.push_back(num % 2);
        num = num / 2;
    }
    int s = vect.size();
    int i = s - 1;
    for (i = s - 1; i >= 0; i--)
    {
        cout << vect.at(i);
    }
    return vect.at(i);
}

int main()
{
    int a, b;
    cout << "Enter first number: " << endl;
    cin >> a;
    cout << "Enter second number: " << endl;
    cin >> b;

    int a_deci = BinaryToDecimal(a);
    int b_deci = BinaryToDecimal(b);

    int sum = a_deci + b_deci;

    DecimalToBinary(sum);
    cout << endl;
    return 0;
}    

Output:

Enter first number: 
10101
Enter second number:
11010
101111terminate called after throwing an instance of 'std::out_of_range'what():  vector::_M_range_check: __n (which is 18446744073709551615) >= this->size() (which is 6)

What does this error message mean and how do I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

衣神在巴黎 2025-02-18 07:01:14

在此之后,

for (i = s - 1; i >= 0; i--)
{
    cout << vect.at(i);
}

变量i等于-1

因此,成员函数的下一个呼叫的值等于-1(该值获得了很大数量的无符号类型std :: vector&lt&gt ; :: size_type

return vect.at(i);

抛出异常。

看来您需要从函数返回整个向量元素,其整个向量元素将代表二进制形式的数字。

而不是容器std :: vector&lt;最好使用std :: bitset

After this for loop

for (i = s - 1; i >= 0; i--)
{
    cout << vect.at(i);
}

the variable i is equal to -1.

So the next call of the member function at with the value equal to -1 (that yields a very big number of the unsigned type std::vector<int>::size_type)

return vect.at(i);

throws the exception.

It seems you need to return from the function the whole vector elements of which will represent a number in the binary form.

Instead of the container std::vector<int> it will be better to use std::bitset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文