C++ 应用程序使用sort函数崩溃

C++ 应用程序使用sort函数崩溃,c++,sorting,crash,genetic-algorithm,C++,Sorting,Crash,Genetic Algorithm,我有一个程序,应该是发展一个IA。我尝试过做一些类似遗传算法的事情(主要步骤是:-选择最佳种群,-变异种群,繁殖种群)。为了选择最佳种群,我想对它们进行排序,并考虑最好的(考虑顺序函数)。 我使用了std::sort函数,但有时它会崩溃,但我找不到原因 由于我在这个项目上受阻,我真的不知道我应该展示多少。以下是主要观点: 我定义了一个IA(带有一些参数): 然后我想让它做50个进化步骤: ia.evolve(50); 深入查看排序函数(调试),有时会出现以下状态: 其中“最后一个元素”只包

我有一个程序,应该是发展一个IA。我尝试过做一些类似遗传算法的事情(主要步骤是:-选择最佳种群,-变异种群,繁殖种群)。为了选择最佳种群,我想对它们进行排序,并考虑最好的(考虑顺序函数)。 我使用了
std::sort
函数,但有时它会崩溃,但我找不到原因

由于我在这个项目上受阻,我真的不知道我应该展示多少。以下是主要观点:

我定义了一个IA(带有一些参数):

然后我想让它做50个进化步骤:

ia.evolve(50);

深入查看排序函数(调试),有时会出现以下状态:

其中“最后一个元素”只包含不可能的东西(意思是“出乎意料的(对我来说)东西”)

由于
g
(游戏)对象不包含正确的内容,因此我给出了以下代码和相关代码(即使可能根本不是原因):

这是我的
g
(游戏)构造函数:

Game::Game() {
     nextBarX = BAR_SPACING;
     speed = 0.;
     ySpacing = Y_SPA;
     currentY = GAME_HEIGHT / 2.0;

    passedBars = 0;
    //std::cout << "[Game] Default ctor" << std::endl;
    centerY = std::vector<double>(5);
    centerY[0] = 30.0;
    centerY[1] = 30.0;
    centerY[2] = 30.0;
    centerY[3] = 30.0;
    centerY[4] = 30.0;
}
在Simul中,我定义了:

bool operator<( Simul& v) ;

我期待着得到一些建议或想法,了解到底是什么导致了应用程序崩溃。

您的
运算符在您的运算符重载中,您是否应该为
播放
返回
true
?现在我认为您正在检查它是否大于,而不是小于。假设函数reset(),询问函数press()和step()是否是确定性的,并最终增加私有的g.score值。(g.getScore()只返回score_value)。然后Simul::play()函数实现一个严格弱排序,是吗?作为严格弱排序调试构建验证的一部分,编译器生成代码来检查
a
!(B
是正确的。更改比较中使用的值从来都不是一个好主意。您最终不知道将比较哪些顺序元素,或者在哪些元素上运行多少比较。
Game& Game::operator=(Game rhs) {
    //std::cout << "[Game] Assignment operator" << std::endl;
        this->centerY = std::vector<double>(5);
    this->centerY = rhs.centerY;
    this->currentY = rhs.currentY;
    this->nextBarX = rhs.nextBarX;
    this->passedBars = rhs.passedBars;
    this->speed = rhs.speed;
    this->ySpacing = rhs.ySpacing;


    return *this;
}
void Game::reset(){
    nextBarX = BAR_SPACING;
    speed = 0.;
    ySpacing = Y_SPA;
    currentY = GAME_HEIGHT / 2.0;


    centerY = std::vector<double>(5);
    centerY[0] = 30.0;
    centerY[1] = 30.0;
    centerY[2] = 30.0;
    centerY[3] = 30.0;
    centerY[4] = 30.0;  passedBars = 0;
}
Game& Game::operator=(Game rhs) {
    //std::cout << "[Game] Assignment operator" << std::endl;
        this->centerY = std::vector<double>(5);
    this->centerY = rhs.centerY;
    this->currentY = rhs.currentY;
    this->nextBarX = rhs.nextBarX;
    this->passedBars = rhs.passedBars;
    this->speed = rhs.speed;
    this->ySpacing = rhs.ySpacing;


    return *this;
}
class IA {
private:
    std::vector<Simul> sim_;
}
void IA::sortIA() {
    std::sort(sim_.begin(), sim_.end());
}
bool operator<( Simul& v) ;
bool Simul::operator<( Simul& v) 
{
    if (play() > v.play()){
        return true;
    }
    else{
        return false;
    }
}
int Simul::play(){
    bool stillPlaying = true;
    g.reset();

    while (stillPlaying){
        //g.display();
        bool pressed = ask_if_press();
        stillPlaying = !g.step(pressed);
        if (g.getScore() > 100){
            return g.getScore();
        }
    }
    return g.getScore();
}