pop_back()导致"向量下标超出范围"

时间:2017-03-07 06:02:20

标签: c++

我正在制作一个程序只是为了练习向量,这让我很难过。该计划的目的是为了帮助人们在餐馆之间进行选择。

以下是导致错误的代码:

#include <iostream>
#include <vector>
#include <string>

const int ITEMS_PER_MATCHUP = 2;

using namespace std;

int PromptChoice(string first, string second, string helpMessage = "") {
    string input;
    while (true) {
        cout << "Which restaurant do you prefer, " << first << "(1) or " << second << "(2)? ";
        cin >> input; //no need to protect as it is going into a string variable
        if (input == "first" || input == "1" || input == "left" || input == "First" || input == "Left") {
            return 0;
        }
        else if (input == "second" || input == "2" || input == "right" || input == "Second" || input == "Right") {
            return 1;
        }
        // if the response was not recognized
        else cout << endl << helpMessage << endl << "Please enter 1 or 2" << endl << "Please try again." << endl << endl;
    }
}

int main() {
    vector<string> list = { "Burger Place", "Italian Place", "Soup And Salad Place", "BBQ Place" };

    vector<int> stillInTheRunning;
    for (int index = 0; index < list.size(); index++) { //populates an array full of values 0 to the size of the array
        stillInTheRunning.push_back(index);
    }

    int matchupsThisRound = stillInTheRunning.size();

    for (int i = 0; i < matchupsThisRound; i += ITEMS_PER_MATCHUP) { //ITEMS_PER_MATCHEP == 2

        int choice = PromptChoice(list[stillInTheRunning[i]], list[stillInTheRunning[i + 1]]);
        //stillInTheRunning.erase(stillInTheRunning.begin() + i + choice);
        stillInTheRunning.pop_back();

    }

    for (int i = 0; i < stillInTheRunning.size(); i++) { //print the vector
        cout << endl << stillInTheRunning[i] << endl;
    }

    system("pause"); //I know I know don't use system. just for debuging?
}

如果我理解错误,当您尝试访问超出范围的矢量索引时,通常会发生错误。大于vector.size() - 1的东西,但在这种情况下,它发生在我尝试使用vector.erase()

想想也许我只是抢了for循环我尝试切换到pop_back(),因为我认为你不能把那个搞砸了。但我仍然有错误。

玩弄它我试着评论一些事情。

就像我注释掉提示功能一样:

for (int i = 0; i < matchupsThisRound; i += ITEMS_PER_MATCHUP) { //ITEMS_PER_MATCHEP == 2
        //int choice = PromptChoice(list[stillInTheRunning[i]], list[stillInTheRunning[i + 1]]);
        //stillInTheRunning.erase(stillInTheRunning.begin() + i + choice);
        stillInTheRunning.pop_back();
    }

没有错误

如果我注释掉pop_back():

for (int i = 0; i < matchupsThisRound; i += ITEMS_PER_MATCHUP) { //ITEMS_PER_MATCHEP == 2
        int choice = PromptChoice(list[stillInTheRunning[i]], list[stillInTheRunning[i + 1]]);
        //stillInTheRunning.erase(stillInTheRunning.begin() + i + choice);
        //stillInTheRunning.pop_back();
    }

也没有错误。

导致问题的原因是什么?

2 个答案:

答案 0 :(得分:2)

我找到了它!

它在这里

PromptChoice(list[stillInTheRunning[i]], list[stillInTheRunning[i + 1]])

在for循环的第二次运行中i = 2; 从背面弹出一个项目之后,向量的大小现在是3而不是4,所以stillInTheRunning [i + 1]试图获取索引3中的项目,现在该项目不存在。

答案 1 :(得分:0)

问题是当你pop_back时,stillInTheRunning的大小减少,但matchupsThisRound保持不变,所以最终循环到达i+1不在的点stillInTheRunning的范围。

同样有点奇怪的是,你每个周期将i增加2,但pop_back只增加一次。