std :: sort崩溃 - 排序没有严格的弱排序

时间:2018-02-25 09:41:00

标签: c++ sorting strict-weak-ordering

我正在尝试对项目的矢量进行排序。如代码注释中所述,顺序应为:

拥有更多行动点(mAp)的参与者先行。当存在平局时,与战斗发起人(mDisposition)具有相同处置(mBattleInitiator)的参与者将首先出现。

以下代码(简化示例)在macOS上崩溃,可能是由于我的排序实现不正确:

#include <QtCore>

class AiComponent
{
public:
    enum Disposition {
        Friendly,
        Hostile
    };

    AiComponent(Disposition disposition) : mDisposition(disposition) {}
    ~AiComponent() { qDebug() << "Destroying AiComponent"; }

    Disposition mDisposition;
};

class BattleManager
{
public:
    BattleManager() : mBattleInitiator(AiComponent::Hostile) {}

    class Turn {
    public:
        Turn() : mAp(1) {}

        Turn(QSharedPointer<AiComponent> aiComponent) :
            mAiComponent(aiComponent),
            mAp(1)
        {
        }

        Turn(const Turn &rhs) :
            mAiComponent(rhs.mAiComponent),
            mAp(1)
        {
        }

        QSharedPointer<AiComponent> mAiComponent;
        int mAp;
    };

    void addToTurnQueue(QSet<QSharedPointer<AiComponent>> aiComponents);

    AiComponent::Disposition mBattleInitiator;
    QVector<Turn> mTurnQueue;
    Turn mActive;
};

void BattleManager::addToTurnQueue(QSet<QSharedPointer<AiComponent> > aiComponents)
{
    foreach (auto aiComponent, aiComponents) {
        mTurnQueue.append(Turn(aiComponent));
    }

    // Sort the participants so that ones with more action points (mAp) go first.
    // When there is a tie, participants with the same disposition (mDisposition)
    // as the initiator of the battle (mBattleInitiator) go first.
    std::sort(mTurnQueue.begin(), mTurnQueue.end(), [=](const Turn &a, const Turn &b) {
        if (a.mAp > b.mAp)
            return true;

        if (a.mAp < b.mAp)
            return false;

        // At this point, a.mAp is equal to b.mAp, so we must resolve the tie
        // based on mDisposition.
        if (a.mAiComponent->mDisposition == mBattleInitiator)
            return true;

        if (b.mAiComponent->mDisposition == mBattleInitiator)
            return false;

        return false;
    });
}

int main(int /*argc*/, char */*argv*/[])
{
    BattleManager battleManager;

    for (int i = 0; i < 20; ++i) {
        qDebug() << "iteration" << i;

        QSet<QSharedPointer<AiComponent>> participants;

        AiComponent::Disposition disposition = i % 2 == 0 ? AiComponent::Hostile : AiComponent::Friendly;
        QSharedPointer<AiComponent> ai(new AiComponent(disposition));
        participants.insert(ai);

        battleManager.addToTurnQueue(participants);
    }

    // This should print (1 1), (1 1), ... (1 0), (1 0)
    foreach (auto turn, battleManager.mTurnQueue) {
        qDebug() << "(" << turn.mAp << turn.mAiComponent->mDisposition << ")";
    }

    return 0;
}

我已经查看了有关该主题的其他答案。他们中的大多数人只是说“将它实现为&gt; b”,这在我的情况下不起作用。有一些似乎相关,但没有帮助我:

实现我追求目标的最简单方法是什么?

1 个答案:

答案 0 :(得分:1)

我会在您的代码中删除评论并解释它的错误(如果有的话),以及如何修复它。

// Sort the participants so that ones with more action points (mAp) go first.

到目前为止很好

// When there is a tie, participants with the same disposition (mDisposition) as the initiator of the battle (mBattleInitiator) go first.

如果两个参与者的处置方式与发起人相同,会怎样?即使您可以保证没有2个元素满足这个条件,也允许排序算法将元素与自身进行比较。在这种情况下,此测试将返回true,违反了严格弱排序的条件之一,即元素必须与自身进行比较(即compare(a,a)必须始终为false)。

或许您想说,如果a与发起人具有相同的处置方式,而b没有,则a应被视为小于b。这可以编码为:

return dispositionOfA == mBattleInitiator && dispsitionOfB != mBattleInitiator;

所以你的完整测试看起来像这样:

if (a.mAp > b.mAp)
    return true;
if (a.mAp < b.mAp)
    return false;

return a.mAiComponent->mDisposition == mBattleInitiator &&
       b.mAiComponent->mDisposition != mBattleInitiator;