C ++根据成员布尔值对向量中的对象进行排序
C++ Sorting Objects in a vector based on member boolean
在我的程序中,我有 类 用于在游戏中处理射弹。
class Projectile
{
bool IsActive;
bool GetActive();
//....
};
class Game
{
std::vector<Projectile*> ProjectilesToUpdate;
//....
};
当然,还有更多,但我正在努力解决我当前的问题。
我想使用 std::sort 使所有 IsActive == true 的射弹都在最开始,而任何未激活的射弹都在最后。
我该怎么做?
基本上,您想创建一个 partition:
std::partition(std::begin(ProjectilesToUpdate),
std::end(ProjectilesToUpdate),
[](Projectile const* p) { return p->GetActive(); }
);
关于附属问题:
I had to remove the "const" part in the code to make it compile.
那是因为你的 GetActive()
方法应该是 const:
bool GetActive() const { return IsActive; }
见Meaning of "const" last in a C++ method declaration?
how can I use this to delete every single object (and pointer to object) that is no longer needed?
您可以使用智能指针(例如std::shared_ptr
)而不再关心删除。因此,您可以按如下方式使用 Erase–remove idiom:
std::vector<std::shared_ptr<Projectile>> ProjectilesToUpdate;
// :
// :
auto it = std::remove_if(
std::begin(ProjectilesToUpdate),
std::end(ProjectilesToUpdate),
[](std::shared_ptr<Projectile> const& p) { return !p->GetActive(); } // mind the negation
);
ProjectilesToUpdate.erase(it, std::end(ProjectilesToUpdate));
相关问题:What is a smart pointer and when should I use one?
如果你不想使用智能指针,你可以使用返回的迭代器,它指向第二组的第一个元素(即非活动元素)并迭代到数组的末尾:
auto begin = std::begin(ProjectilesToUpdate);
auto end = std::end(ProjectilesToUpdate);
auto start = std::partition(begin, end,
[](Projectile const* p) { return p->GetActive(); }
);
for (auto it = start; it != end; ++it) {
delete *it;
}
ProjectilesToUpdate.erase(start, end);
请注意,我没有在循环内调用擦除,因为它会使迭代器无效。
当然,最后一个解决方案比使用智能指针更复杂。
在我的程序中,我有 类 用于在游戏中处理射弹。
class Projectile
{
bool IsActive;
bool GetActive();
//....
};
class Game
{
std::vector<Projectile*> ProjectilesToUpdate;
//....
};
当然,还有更多,但我正在努力解决我当前的问题。
我想使用 std::sort 使所有 IsActive == true 的射弹都在最开始,而任何未激活的射弹都在最后。
我该怎么做?
基本上,您想创建一个 partition:
std::partition(std::begin(ProjectilesToUpdate),
std::end(ProjectilesToUpdate),
[](Projectile const* p) { return p->GetActive(); }
);
关于附属问题:
I had to remove the "const" part in the code to make it compile.
那是因为你的 GetActive()
方法应该是 const:
bool GetActive() const { return IsActive; }
见Meaning of "const" last in a C++ method declaration?
how can I use this to delete every single object (and pointer to object) that is no longer needed?
您可以使用智能指针(例如std::shared_ptr
)而不再关心删除。因此,您可以按如下方式使用 Erase–remove idiom:
std::vector<std::shared_ptr<Projectile>> ProjectilesToUpdate;
// :
// :
auto it = std::remove_if(
std::begin(ProjectilesToUpdate),
std::end(ProjectilesToUpdate),
[](std::shared_ptr<Projectile> const& p) { return !p->GetActive(); } // mind the negation
);
ProjectilesToUpdate.erase(it, std::end(ProjectilesToUpdate));
相关问题:What is a smart pointer and when should I use one?
如果你不想使用智能指针,你可以使用返回的迭代器,它指向第二组的第一个元素(即非活动元素)并迭代到数组的末尾:
auto begin = std::begin(ProjectilesToUpdate);
auto end = std::end(ProjectilesToUpdate);
auto start = std::partition(begin, end,
[](Projectile const* p) { return p->GetActive(); }
);
for (auto it = start; it != end; ++it) {
delete *it;
}
ProjectilesToUpdate.erase(start, end);
请注意,我没有在循环内调用擦除,因为它会使迭代器无效。
当然,最后一个解决方案比使用智能指针更复杂。