Another C ++ 11 way:
list<Fruit*> Apples; list<Fruit> Basket; Basket.remove_if([](const Fruit& fruit) { return any_of(Apples.begin(), Apples.end(), [](Fruit* apple) { return &fruit == apple; }); });
Now change the first container to save the iterators to the second:
list<list<Fruit>::iterator> Apples; list<Fruit> Basket; for (auto apple : Apples) Basket.erase(apple);
This way you get better performance and don't change your interface very little or no, since iterators behave like pointers in most cases.
Also take a look at this: Should std :: list be deprecated?
Note that for both solutions to work, the Basket container must be std::list .
source share