c++ - 程序忽略 while 循环内容,当强制访问循环内的方法时崩溃

标签 c++

我正在为具有以下描述的类编写程序:

“夏娃是一位美丽的公主,许多追求者都来追求她。她决定按照以下程序来决定她嫁给谁。她会把男人排成一行,并根据他们的位置给他们一个编号(1代表第一位)排到第二个2,以此类推。每当她排到第三个人时,该人就会被淘汰出线。当她走到线尾时,她会从头开始继续数。最后一个人排队将有幸与她结婚。”

问题是,每次我运行代码时,我都会得到一个非常大的数字作为我的答案。使用 cout 进行测试表明该程序没有触及 while 循环的内容。删除 while 循环后,程序崩溃并显示“vector 迭代器 + 偏移量超出范围”和“标准 C++ 库超出范围”。

我知道 while 循环本身有问题导致程序崩溃,我已经研究了好几天,试图弄清楚是什么原因。我不明白的是为什么程序首先忽略了 while 循环。任何和所有建议将不胜感激。

// EveSuitor.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;

class EveSuitor
{
    vector<int> newList;

public:

    EveSuitor::EveSuitor(vector<int> list)
    {
    vector<int> newList = list;
    }


    void setUpSuitors(vector<int>list);
    int chooseWinner(vector<int>list);

    int suitorCounter;
    int suitorTotal;

};



void EveSuitor::setUpSuitors(vector<int>v)
{
    int numOfSuitors;
    cout<< "Enter in the number of suitors that are there: ";
    cin>> numOfSuitors;

    for (int i = 0; i < numOfSuitors; i++)
    {
        v.push_back(i);
    }

    for (unsigned int j = 0; j < v.size(); j++)
    {
        cout<< v[j] << " ";
    }
}
int EveSuitor::chooseWinner(vector<int>v)
{
    while (v.size() > 1)
    {
        v.erase(v.begin()+suitorTotal);

        for (unsigned int j = 0; j < v.size(); j++)
        {
            cout<< v[j] << " ";
        }   

        suitorTotal = suitorTotal + suitorCounter;

        if (suitorTotal > v.size())
        {
            suitorCounter = suitorTotal - v.size();//This will reset the counter for the suitor, so that it will move on to the next suitor at the beginning of the list.
            suitorTotal = 0;
        }
        else
        {
            suitorCounter = suitorCounter + 3;
            suitorTotal = suitorTotal + 3;
        }
    }
    if (v.size() == 1)
    {
        return v[0];
    }

}

int _tmain(int argc, _TCHAR* argv[])
{

    int suitorTotal = 3;
    int suitorCounter = 3;
    vector<int>listOfSuitors;

    EveSuitor list(listOfSuitors);

    list.setUpSuitors(listOfSuitors);
    int winner = list.chooseWinner(listOfSuitors);

    cout<<"The position you should be at is: " << winner;
    return 0;
}

最佳答案

您的列表未被填充。你的setUpSuitors函数的签名为 setUpSuitors(vector<int> v) ,这意味着 vector将按值传递,也就是说,该函数将有权访问 vector 的拷贝。如果你想修改函数内部的 vector ,你需要通过引用传递它,像这样:setUpSuitors(vector<int>& v) (注意 & )。

你得到一些可笑数字的原因是你没有从 chooseWinner 返回,因为你采取了 if 的错误分支来保护 return v[0];。陈述。 (实现细节导致使用伪随机值而不是返回值。)


至于崩溃:

错误消息告诉您问题发生在尝试使用 v.erase(v.begin()+suitorTotal); 从 vector 中删除时.这有两个问题

1) suitorTotal 都不是和 suitorCounter实际上是在类内部初始化的。例如,您可以将它们传递到构造函数中以解决此问题,或者在之后设置它们。

2) 除非名称具有误导性,v.begin() + suitorTotal会给你相当于 v.end() 的迭代器, 这在 erase 中不起作用.您需要重新考虑选择接下来要删除哪个追求者的逻辑。

关于c++ - 程序忽略 while 循环内容,当强制访问循环内的方法时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21616511/

相关文章:

c++ - `clang-check` 是否未能兑现 `-isystem` ?

c++ - 带 float 的零符号

c++ - 错误 : expected unqualified-id before 'not' token g c++ builder

c++ - 数组中的唯一整数

c++ - 如何反转可变参数模板函数的参数顺序?

c++ - 为什么我不能重载 operator=?

c++ - const 对象的复制构造函数

c++ - 谁能告诉我为什么 float 不能容纳 3153600000?

C++:构造函数、继承和初始化

c++ - 如何使用软件程序(如yahoo messenger)向手机发送短信?