我正在写一个八叉树算法。在函数内部我遍历八叉树。我得到节点指针和球体作为输入。我检查节点是否应该保持球体然后我想将它添加到节点s object list and remove it from its parent
的列表中。以下是代码
functionBody()
{
.....
if (!node->objectList.empty())
node->objectList.erase(std::remove_if(node->objectList.begin(), node->objectList.end()-1 , [&t](auto& temp) { return temp == t; }));
...
}
typedef struct Sphere
{
Sphere() = default;
Sphere(const Vector3 centre_, const float radius_, const Material& material_) : centre(centre_), radius(radius_), material(material_)
{
assert(radius != 0);
invRadius = 1.0 / radius;
Vector3 radiusOffset = Vector3(radius);
aabb.min = Vector3(centre - radiusOffset);
aabb.max = Vector3(centre + radiusOffset);
}
bool operator==(const Sphere& rhs)
{
return (centre == rhs.centre) && (radius == rhs.radius);
}
Vector3 centre;
float radius;
float invRadius;
Material material;
AABB aabb;
}Sphere;
正如你所看到的,我已经operator==
定义了 Sphere。
我看到remove_if
即使谓词返回错误,它也会删除元素。
例如,第一次迭代它找到一个球体t
并使用remove_if
. 这t
最后出现在向量中。现在考虑父母在其向量中仍然有3个球体,但是,当我现在去其他孩子时,我们仍然尝试t
在父母中搜索并且remove_if
仍然删除最后一个条目。我不明白为什么?