I have the following sorting algorithm, which sorts an std::vector
of unique armor_set
pointers. By some property of my sorting algorithm, it chokes up and runs into undefined behavior which ends up comparing a valid lhs
to a rhs
which is a nullptr
.
Despite moving the algorithm around multiple times, I've been unable to discern the problem. I feel as though I'm missing some sort of simple rule I should be following regarding how this std::sort
algorithm works.
Any help would be appreciated.
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;
}
});
See Question&Answers more detail:os