C++ bughunt - vector 中的高分插入会使程序崩溃

标签 c++ debugging

我正在开发一款游戏。我的玩家存储在 vector 中,在游戏结束时,当尝试将高分插入正确位置时游戏崩溃。

这是我的(请忽略葡萄牙语评论,代码非常简单:P):

//TOTAL_HIGHSCORES is the max. number of hiscores that i'm willing to store. This is set as 10.
bool Game::updateHiScores()
{
    bool stopIterating;
    bool scoresChanged = false;
    //Se ainda nao existirem TOTAL_HISCORES melhores pontuacoes ou se a pontuacao for melhor que uma das existentes
    for (size_t i = 0; i < players.size(); ++i)
    {

    if (hiScores.empty())
        checkForValues = true;
    else
        checkForValues = (hiScores.size() < TOTAL_HISCORES || hiScores.back() < players[i].getScore());
    if (players[i].getScoreValue() > 0  && checkForValues)
        {
            scoresChanged = true;

            if(hiScores.empty() || hiScores.back() >= players[i].getScore())
                hiScores.push_back(players[i].getScore());
            else
            {   
                //Ciclo que encontra e insere a pontuacao no lugar desejado
                stopIterating = false;
                for(vector<Score>::iterator it = hiScores.begin(); it < hiScores.end() && !(stopIterating); ++it) 
                {
                    if(*it <= players[i].getScore())
                    {
                        //E inserida na posicao 'it' o Score correspondente
                        hiScores.insert(it, players[i].getScore());
                        //Verifica se o comprimento do vector esta dentro do desejado, se nao estiver, este e rectificado
                        if (hiScores.size() > TOTAL_HISCORES)
                            hiScores.pop_back();
                        stopIterating = true;
                    }
                }
            }
        }
    }

    if (scoresChanged)
        sort(hiScores.begin(), hiScores.end(), higher);

    return scoresChanged;
}

我在这里做错了什么?

谢谢你的时间,伙计们。

编辑:

我最终将我的代码简化为:

bool scoresChanged;

vector<Score> hiScoresCopy = hiScores;

for (size_t i = 0; i < TOTAL_PLAYERS; ++i)
    hiScoresCopy.push_back(players[i].getScore());

sort(hiScoresCopy.begin(), hiScoresCopy.end(), higher);

scoresChanged = (hiScores != hiScoresCopy);

if (scoresChanged)
{
    if (hiScoresCopy.size() > TOTAL_HISCORES)
        hiScoresCopy.resize(TOTAL_HISCORES);
    hiScores = hiScoresCopy;
}

return scoresChanged;

最佳答案

您没有确切说明代码崩溃的位置或崩溃的性质,所以我会猜测。

这个测试是错误的方法。

it < hiScores.end() && !(stopIterating)

在一种常见情况下,您的迭代器 it将在您设置的同一子句中失效 stopIterating为真。

您可以这样做(对于迭代器,!=< 更符合惯用语,尽管两者都适用于 vector )。

!stopIterating && it != hiScores.end()

或者,在插入之后你可以只得到 break;声明并取消 stopIterating多变的。我认为这对于代码的大多数读者来说会更清楚。

关于C++ bughunt - vector 中的高分插入会使程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2888906/

相关文章:

c++ - 如何使 GDB 更快

java - 在 Eclipse 调试时更改长数组的值

c - 值(value)观是什么?

python - 调试python时如何重定向标准输入?

node.js - 在node.js中使用npm调试包登录时如何显示时间戳?

c++ - 在 C++ 程序中使用 _asm 代码是否有所不同

vis studio 2008 中的 c++ 调试,如何在变量变为零时中断

有两个类的 C++ 导入循环?

c++ - 使用 ifstream 读取字节

c++ - iOS 自定义框架链接器错误