I had a little problem finding an element in a vector shared_ptr.
Here is what I ended up with:
std::vector<std::shared_ptr<Block>> blocks;
bool contains(Block* block) {
for (auto i = blocks.begin(); i != blocks.end(); ++i) {
if ((*i).get() == block) {
return true;
}
}
return false;
}
However, I was not able to do this with std::findor even std::find_if. Is there another C ++ compatible way to achieve this?
EDIT : this is the code I have after the answer:
bool contains(Block* block) {
auto found = std::find_if(blocks.begin(), blocks.end(), [block](std::shared_ptr<Block> const& i){
return i.get() == block;
});
return found != blocks.end();
}
source
share