我有以下排序算法,它可以对唯一的
尽管我多次移动算法,但我无法确定问题所在。我感觉我缺少应该遵循的某种简单规则,关于这个
任何帮助都将不胜感激。
armor_set
指针的std::vector
进行排序。由于某些排序算法的属性,它会出现问题并导致未定义的行为,最终将一个有效的lhs
与一个nullptr
的rhs
进行比较。尽管我多次移动算法,但我无法确定问题所在。我感觉我缺少应该遵循的某种简单规则,关于这个
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;
}
});
armor_sets
的定义吗? - Davidnullptr
。 - Colin Basnett