Boost logo

Boost Users :

From: Jonathan Brownell (alphaomega_at_[hidden])
Date: 2002-02-10 01:46:39

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, std::greater>());

int minimum = std::for_each(&array[0], &array[10],
        select_by_predicate<int, std::less>());

assert(maximum == 9 && minimum == 0);

Another example, using select_by_predicate independently:

select_by_predicate<int, std::greater> max_error(0);

for(loop through data samples)
        double error;
        ... analyze data sample 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.


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at