Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

Let’s consider such method:

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)
        {

        });
}

what I’m trying to achieve is to delete organism that iterator points to from vector<unique_ptr<Organism>>, so how am I supposed to compare unique_ptr<Organism> to std::vector<unique_ptr<Organism>>::iterator?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You don’t have to search through the vector to find an iterator; you just have to erase it:

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

Or if you want to delete the unique_ptr and not only the element it points to:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading