矢量和没有产生结果

时间:2018-03-29 12:11:13

标签: c++ for-loop vector runtime-error abort

我正在开发一个简单的游戏(摇滚,纸张,剪刀),当我尝试使用另一个向量的元素作为条件来填充向量时,我遇到了这个问题。代码可能更容易理解:

#include "stdafx.h"
#include "../../Library/std_lib_facilities.h"

// With the fibonacci series I can generate a secret sequence of number
int fib(int n){
    if (1 == n || 2 == n) {
        return 1;
    }
    else {
        return fib(n - 1) + fib(n - 2);
    }
}

// It shows the list of element in a given vector
void showVector(vector<int>myVector, string nameVector) {
    cout << "\n\n";
    cout << nameVector << " Debug: | ";
    for (int i = 1; i < 10; i++) {
        cout << myVector[i] << " | ";
    }
    cout << "\n\n";
}

// String variant
void showVector(vector<string>myVector, string nameVector) {
    cout << "\n\n";
    cout << nameVector << " Debug: | ";
    for (int i = 1; i < 10; i++) {
        cout << myVector[i] << " | ";
    }
    cout << "\n\n";
}

// Generate a sequence of number in a vector
vector<int> generateCode(int input) {

    vector<int>myVector;

    for (int i = 1; i <= 10; i++) {
        myVector.push_back(fib(i + input) % 3);
    }

    return myVector;
}

int main()
{
    // Secret sequence of moves, based on the value (0, 1 or 2) i will show Rock, Paper or Scissor
    vector<int>fibSeries;
    vector<string>movSeries;

    // Initialization and settings
    int input = 0;

    cout << "Digit an integer to start: ";
    while (cin >> input) {

        // Check the number to make sure is a valid one (i will implement a check later) and generate the secret code
        fibSeries = generateCode(input);

        // For each 0 i'll put Rock in the vector, same for 1 ( in this case paper ) and 2 ( scissor )
        for (int i = 0; i <= fibSeries.size(); i++) {
            if (fibSeries[i] == 0){
                movSeries.push_back("Rock");
            }
            else if (fibSeries[i] == 1) {
                movSeries.push_back("Paper");
            }
            else if (fibSeries[i] == 2) {
                movSeries.push_back("Scissor");
            }
            else {
                movSeries.push_back("Rock");
            }
        }

        // Shows the vector graphically, for debug.
        showVector(fibSeries, "fibisSeries");
        showVector(movSeries, "movSeries");

        // So for a combination of      1 - 2 - 0 - 1
        // The result should be:        Paper - Scissor - Rock - Paper

    }

    return 0;
}

执行代码后,我得到一个(Abort)。我不明白,我是C ++的新手所以请原谅我是否是一个愚蠢的错误。大多数代码很复杂,因为我有遵循的规则,所以例如,如果我没有学习一些我不能在这里使用它的东西。我只是想知道为什么代码不希望我使用带向量的for!

2 个答案:

答案 0 :(得分:0)

  

for(int i = 0; i&lt; = fibSeries.size(); i ++)

正如@Yola所提到的,你迭代一个元素比你拥有的更多:

如果您有向量std::vector<char> v = {'a', 'b', 'c'};,则可以按以下方式访问元素:

v[0] (-> 'a')
v[1] (-> 'b')
v[2] (-> 'c')

通常,您的最高索引为length - 1

for (int i = 0; i <= fibSeries.size(); i++)

从0迭代到小于或等于到size();也就是说,0,1,2,3(4个元素,在3个元素向量中)。

正确的代码应该是:

for (int i = 0; i < fibSeries.size(); i++)
                 ^^^

答案 1 :(得分:0)

正如已经说过的那样,您的问题是您应该始终使用< size,因为索引为0-based <= size会读取已分配空间的末尾。

但是如果可能的话,有几条评论,引入了一种新的range based for-loops样式,它将使你不必编写条件和迭代表达式:

map<int, string> movSeries = { { 0, "Rock"s }, { 1, "Paper"s }, { 2, "Scissors"s } };
vector<int> fibSeries(10);
auto input = 0;

while(cin >> input) {
    for(auto& i : fibSeries) {
        i = fib(input++) % 3;
    }

    cout << "\n\nDebug | ";
    copy(cbegin(fibSeries), cend(fibSeries), ostream_iterator<int>(cout, " | "));
    cout << "\n\n\n\nDebug | ";

    for(const auto i : fibSeries) {
        cout << movSeries[i] << " | ";
    }
    cout << endl << endl;
}