gpt4 book ai didi

c++ - 使用vector时出错:what():vector::_ M_range_check

转载 作者:行者123 更新时间:2023-12-02 09:56:09 25 4
gpt4 key购买 nike

我是一名初学者程序员,并且正在尝试编写一个程序,询问用户以下内容:

  • P-打印数字
  • A-添加数字
  • M-显示数字的平均值
  • S-显示最小数字
  • L-显示最大数字
  • Q-退出

  • 用户必须键入这些字母(无论是小写还是大写)才能使用它。目的是仅使用 vector 创建数字列表并执行上述操作

    例如,如果我键入p,它应该显示列表为空(如果列表中没有数字)或以以下格式显示数字:[1 2 3]

    在此处将相同格式应用于其他选择

    这是我自己编写的代码:
    #include <iostream>
    #include <vector>
    using namespace std;

    int main()
    {
    int Count {1};
    char selection {};
    int Numbers {};
    double Sum {0};
    double Average {};
    int Minimum {};
    int Maximum {};
    vector <int> list;
    do {
    cout << "P - Print Numbers" << endl;
    cout << "A - Add Numbers" << endl;
    cout << "M - Display The Mean Of The Numbers" << endl;
    cout << "S - Display The Smallest Number" << endl;
    cout << "L - Display The Largest Number" << endl;
    cout << "Q - Quit" << endl;
    cout << "\nEnter Your Choice: ";
    cin >> selection;
    switch(selection){
    case 'P':
    case 'p':
    if(list.size() == 0){
    cout << "\n[] - The List Is Empty" << endl;
    } else{
    cout << "\n[ ";
    for(size_t i{0}; i<=list.size(); ++i)
    cout << list.at(i) << " ";
    cout << "]" << endl;
    }
    break;
    case 'a':
    case 'A':
    cout << "\nEnter Your Number: ";
    cin >> Numbers;
    if (Count == 1){
    Maximum = Numbers;
    Minimum = Numbers;
    }
    if (Numbers > Maximum)
    Maximum = Numbers;
    if (Numbers < Minimum)
    Minimum = Numbers;
    list.push_back(Numbers);
    cout << "Added " << Numbers << endl;
    Count += 1;
    break;
    case 'm':
    case 'M':
    if (list.size() == 0)
    cout << "Unable To Calculate The Mean - No Data" << endl;
    else {
    for (size_t j{0}; j<=list.size(); ++j){
    Sum += list.at(j);
    Average = Sum / list.size();
    }
    cout << "\nThe Mean Is : " << Average << endl;
    }
    break;
    case 's':
    case 'S':
    if (list.size() == 0)
    cout << "Unable To Determine The Smallest Number - List Is Empty" << endl;
    else
    cout << "\nThe smallest number is : " << Minimum << endl;
    break;
    case 'l':
    case 'L':
    if (list.size() == 0)
    cout << "Unable To Determine The Largest Number - List Is Empty" << endl;
    else
    cout << "\nThe Largest number is : " << Maximum << endl;
    break;
    case 'q':
    case 'Q':
    cout << "Goodbye!!" << endl;
    break;
    default:
    cout << "Unknown Selection, Please Try Again!" << endl;
    }
    } while (selection != 'q' && selection != 'Q');
    return 0;
    }

    问题是我可以成功添加号码,但是当我尝试打印列表时我得到以下信息:
        terminate called after throwing an instance of 'std::out_of_range'
    what(): vector::_M_range_check: __n (which is 1) >= this->size() (which is 1)

    当然,现在我已经说过我可以在没有任何错误或警告的情况下运行它了。我还尝试将 size_t更改为 int,但是我收到了两个警告...并且我也使用 .push_back()作为 vector ,因此我没有空 vector ...

    任何帮助都会很棒..谢谢:)

    最佳答案

    for (size_t j{0}; j<=list.size(); ++j)中的小于或等于比较不正确。

    考虑最后一次迭代... j将等于list.size()

    相反,对列表元素的规范迭代是:

        for (size_t i{0}; i < list.size(); ++i)
    std::cout << list[i] << " ";

    如上所示,您可以将 list.at(i)更改为 list[i],因为您知道索引将是有效的。

    最后,我建议不要对 vector 使用名称 list,因为这是C++标准库中的链接列表类型的名称。

    关于c++ - 使用vector时出错:what():vector::_ M_range_check,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60006936/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com