So I have 2 std::maps <string, shared_ptr<file> > one is ‘old’ one is ‘new’ I want to get what files were removed and so be capable to iterate thrue differene and do some stuff to shared_ptr. Is such thing possible and how to do it?
So I have 2 std::map s <string, shared_ptr<file> > one is ‘old’ one is
Share
While it’s easy enough to write this yourself (iterate over
Aand check if the key is present inB), this looks like a job forstd::set_difference. We’ll need a lambda or some custom predicate to compare keys, though:If you want to write this yourself, you should consider taking advantage of the fact that both ranges are already sorted, so you can do better than a flat search for existence by advancing two iterators in parallel.
If you don’t have C++11, just use this predicate instead of the lambda:
Beware that there is no comparison on the mapped type! So if you have the same string key in both maps, then there will be no such item in the result, even if the two mapped values differ. If this is undesirable, you need a different output container (e.g. a
std::multimap<my_map::key_type, my_map::mapped_type>) and a different predicate.