C ++ bughunt - 向量中的高分插入会导致程序崩溃

时间:2010-05-22 16:48:59

标签: c++ debugging

我正在玩一款游戏。我的玩家存储在一个向量中,并且在游戏结束时,当试图将高分插入正确的位置时游戏崩溃。

这就是我所拥有的(请忽略葡萄牙语的评论,代码非常简单: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;

5 个答案:

答案 0 :(得分:2)

有几件事:

  • 此代码看起来不对:

    it < hiScores.end()
    

    因为当使用迭代器和end()时,你应该与“!=”而不是“&lt;”进行比较。你想迭代到最后的项目,并且“!= hiScores.end()”是这样做的方式。

  • 您有可用的调试器吗?如果是这样,通过它运行你的代码,然后看看它死了 - 这是一个快速的方法来找出它失败的地方。

答案 1 :(得分:2)

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

这个测试是错误的。

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

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

你可以这样做(!=对于迭代器而言比<更惯用,尽管两者都适用于向量)。

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

或者,在您插入后,您可能只有break;语句并取消stopIterating变量。我认为这对于大多数代码读者来说会更清楚。

答案 2 :(得分:1)

在您的第一个if,您致电hiScores.back(),而不检查hiScores是否为空。

答案 3 :(得分:1)

是否有任何理由无法将代码简化为以下内容?

bool Game::updateHiScores()
{
    for (size_t i = 0; i < players.size(); ++i)
    {
        hiScores.push_back(players[i].getScore());
    }
    sort(hiScores.begin(), hiScores.end(), higher);

    if (hiScores.size() > TOTAL_HIGHSCORES)
    {
        hiScores.erase(hiScores.begin() + TOTAL_HIGHSCORES, hiScores.end());
    }

    return true;
}

答案 4 :(得分:1)

我认为Charles Bailey是对的,但我也认为这段代码不必要地复杂。你可以这样做:

bool Game::updateHiScores()
{
  if(players.empty())
    return(false);

  vector<Score> newScores;
  for(vector<Player>::iterator itr = players.begin() ; itr != players.end(); ++itr)
    newScores.push_back(itr->getScore());

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

  if(hiScores.size() < TOTAL_HISCORES || newScores.front() > hiScores.back())
  {
    hiScores.insert(highScores.end(), newScores.begin(), newScores.end();
    sort(hiScores.begin(), hiScores.end(), higher);
    if(hiScores.size() > TOTAL_HISCORES)
      hiScores.resize(TOTAL_HISCORES);
    return(true);
  }
  return(false);
}