We can use remove_if in C ++ to remove elements from a vector in linear time based on a predicate that works with elements.
bool condition(double d) {...}
vector<double> data = ...
std::remove_if (data.begin(), data.end(), condition);
What if my condition does not depend on values, but on indices? In other words, if I wanted to remove all elements with an odd index or some arbitrary set of indices, etc.?
bool condition(int index) {
vector<double> data = ...
std::remove_if (data.begin(), data.end(), ???);
source
share