Removing elements that match a specified criterium from a vector
Deleting items that match a specific criterium from a C++ vector is not as straightforward as it seems. The solution? First define a function that determines if the element should be deleted:
1
2
3
4
bool bla(child_t &child)
{
return child.first == -1;
}
And then use our friends erase and remove_if:
1
2
std::vector<child_t> children;
children.erase(remove_if(children.begin(), children.end(), bla), children.end());
Easy right.…:)
This post is licensed under CC BY 4.0 by the author.