Quote Originally Posted by superbonzo View Post
... and with C++0x

Code:
#include <algorithm>

int main()
{
    char data[] = {0, 1, 2, 3, 0, 0, 4 ,5 ,0, 0, 0, 6, 7};

    int largest = std::accumulate( data, data + 13, []( int count, char value ){ return value ? 0 : ++count; }, 0 );
}
Well you don't actually need c++0x to do that. The lambda function is just sugar coating. Nicer than writing a one shot functor though, I'll grant you that.

The big flaw though is that your function doesn't actually count the longest consecutive null chars, but just counts the null chars. And if we wanted to do that, I'm sure std::count could do a better job.

Also, but I'm not completely sure, the above could be undefined, as accumulate expects the predicate to "return the result of an accumulation operation", and since in this case lambda(count, value) != lambda(value, count), depending on the implementation, the result could be outrageously different. On my system, I had to swap count and value for it to work correctly.

I doubt an algorithm could do this, as it would require the predicate to have a state (remember the longest chain, for example), and in Scott Meyer's words "Make predicates pure functions". Anything else is undefined with std::algorithm, I believe.