C++ 将元素排序为空

C++ 将元素排序为空,c++,algorithm,sorting,c++11,C++,Algorithm,Sorting,C++11,我有下面的排序算法,它对唯一的armor\u集指针的std::vector进行排序。根据我排序算法的某些属性,它阻塞并运行到未定义的行为中,最终将有效的lhs与rhs进行比较,后者是nullptr 尽管多次移动算法,我还是无法识别问题。我觉得好像我缺少了一些简单的规则,关于这个std::sort算法是如何工作的,我应该遵循这些规则 任何帮助都将不胜感激 std::vector<armor_set*> armor_sets; //insertion of unique armor s

我有下面的排序算法,它对唯一的
armor\u集
指针的
std::vector
进行排序。根据我排序算法的某些属性,它阻塞并运行到未定义的行为中,最终将有效的
lhs
rhs
进行比较,后者是
nullptr

尽管多次移动算法,我还是无法识别问题。我觉得好像我缺少了一些简单的规则,关于这个
std::sort
算法是如何工作的,我应该遵循这些规则

任何帮助都将不胜感激

std::vector<armor_set*> armor_sets;

//insertion of unique armor sets here

std::sort(armor_sets.begin(), armor_sets.end(), [](armor_set* lhs, armor_set* rhs)
{
    auto lhs_collectible_count = collectible_mgr::get().count(lhs->needed_collectible);
    auto rhs_collectible_count = collectible_mgr::get().count(rhs->needed_collectible);

    if(lhs_collectible_count > 0 && rhs_collectible_count == 0)
    {
        return true;
    }
    else if(lhs_collectible_count == rhs_collectible_count)
    {
        return lhs->sort_index > rhs->sort_index;
    }
    else
    {
        auto lhs_collectibles_needed_count = lhs_collectible_count - lhs->collectibles_needed;
        auto rhs_collectibles_needed_count = rhs_collectible_count - rhs->collectibles_needed;

        return lhs_collectibles_needed_count > rhs_collectibles_needed_count;
    }
});
std::向量装甲集;
//在这里插入独特的盔甲套装
std::排序(armor_sets.begin()、armor_sets.end()、[](armor_sets*lhs、armor_sets*rhs)
{
自动lhs\u可收集的\u计数=可收集的\u管理器::get().count(lhs->需要的\u可收集);
auto rhs\u collectible\u count=collectible\u mgr::get().count(rhs->Required\u collectible);
如果(lhs_可回收_计数>0&&rhs_可回收_计数==0)
{
返回true;
}
else if(lhs_可回收_计数==rhs_可回收_计数)
{
返回lhs->sort\u index>rhs->sort\u index;
}
其他的
{
自动lhs\U收藏品所需数量=lhs\U收藏品数量-lhs->收藏品所需数量;
auto rhs\U collectibles\u needed\u count=rhs\u collectible\u count-rhs->collectibles\u needed;
返回lhs\U收藏品所需数量>rhs\U收藏品所需数量;
}
});
比较操作(lambada)就是问题所在。排序中的运算符应确保定义的顺序为严格弱顺序。i、 e

1)For all x, it is not the case that x < x (irreflexivity).
2)For all x, y, if x < y then it is not the case that y < x (asymmetry).
3)For all x, y, and z, if x < y and y < z then x < z (transitivity).
4)For all x, y, and z, if x is incomparable with y, and y is incomparable with z, 
    then x is incomparable with z (transitivity of incomparability).

调用
compare(lhr,rhs)
时,它可能返回
true
false
,具体取决于其他值。调用
compare(rhs,lhs)
时,请注意顺序不同,它将始终返回
true
。不允许
compare(a,b)
compare(b,a)
返回true,这违反了严格弱序的属性

比较函数必须遵循严格的弱顺序

例如,如果我是排序函数,我给你两个armor_集合指针,问你“哪个先到?”然后你返回一个真/假值,指示哪个值先到。然后我给你两个相同的armor_集合指针,但是这次,改变项目的顺序。我问你同样的问题“哪个先来?”。然后返回相同的真/假值。猜猜看,你输了

简而言之,这违反了严格的弱序。没有办法
a
,同时
b
。看看你的比较函数,我猜你违反了这个规则


如果您使用的是VisualStudio,调试运行时会对这种排序冲突进行精确检查。比较函数调用两次,第一次使用A,B顺序,第二次使用B,A顺序。比较每个调用的返回值,如果存在冲突,则会出现assert()。

缺少特定的失败

if(lhs_collectible_count == 0 && rhs_collectible_count > 0)
{
    return false ;
}

这应该是第二次或第三次测试

你确定向量中没有任何
nullptr
s开头吗?@KarlKnechtel-Yep,双重和三重检查。你能显示
armor\u集的定义吗
@Dave-Yep,更新后。比较函数必须遵循严格的弱顺序。例如,如果我是sort函数,我给你两个armor_集合指针,你返回一个真/假值。然后我给你两个相同的armor_集合指针,但顺序相反,然后你返回相同的真/假值。猜猜看,你输了。简而言之,这违反了严格的弱序。没有办法aa@Casey对我犯了一个错误!(一)
if(lhs_collectible_count == 0 && rhs_collectible_count > 0)
{
    return false ;
}