I work in doing programming involving quite a bit of data analysis, and
lately I've gotten sick and tired of doing routine searches on vectors
and other linear containers to pull out maximum/minimum values. I ended
up sitting down and writing a unary function object that retains a value
based on any predicate. An example is worth a thousand words:
int array[10] = { 5, 4, 6, 3, 9, 0, 7, 2, 8, 1 };
int maximum = std::for_each(&array[0], &array[10],
select_by_predicate());
int minimum = std::for_each(&array[0], &array[10],
select_by_predicate());
assert(maximum == 9 && minimum == 0);
Another example, using select_by_predicate independently:
select_by_predicate max_error(0);
for(loop through data samples)
{
double error;
... analyze data sample error ...
max_error.compare(error); // same as max_error(error)
}
std::cout << "Maximum error: " << max_error;
Is select_by_predicate something that boost users would be interested
in? It's been helpful to me in all kinds of contexts.
-Jonathan