如何检查 unique_ptr 是否指向与迭代器相同的对象

how to check if unique_ptr points to the same object as iterator

让我们考虑这样的方法:

void World::remove_organism(organism_iterator organism_to_delete)
{
    remove_if(begin(organisms_vector), end(organisms_vector), [](const unique_ptr<Organism>& potential_organism_to_del)
        {

        });
}

我想要实现的是从 vector<unique_ptr<Organism>> 中删除 iterator 指向的有机体,那么我应该如何比较 unique_ptr<Organism>std::vector<unique_ptr<Organism>>::iterator

您不必搜索 vector 来查找迭代器;你只需要擦除它:

void World::remove_organism(organism_iterator organism_to_delete)
{
    organism_vector.erase(organism_to_delete);
}

或者如果您只想删除 unique_ptr 指向的元素:

void World::remove_organism(organism_iterator organism_to_delete)
{
    organism_to_delete->reset();
}