
From: boostusr@pbjzone.com
On 2/9/2012 11:26 PM, John M. Dlugosz wrote:
On 2/9/2012 3:08 PM, Bill Buklis wrote:
What am I missing? How can count_if be properly expressed as count. To me it seems like it can't.
The value to compare against would be part of the 'pred', either hard-coded into it, or bound as an argument to a function that takes two arguments.
That makes sense, but how would that work? For example, let's say I wanted to count the number of non-zero values in an array. With count_if I can easily do this:
namespace bll = boost::lambda; int data[] = { 1, 0, 2, 0, 3, 0, 4, 0, 5, 0 }; size_t k = boost::count_if(data, bll::_1 != 0);
The documentation implies that this is possible with boost::count using a filtered adaptor. Is it?
This line isn't valid: size_t n = boost::count(data | boost::adaptors::filtered(bll::_1 != bll::_2), 0);
and this line will return 0 matches: size_t n = boost::count(data | boost::adaptors::filtered(bll::_1 != 0), 0);
I'm sure I must be missing the obvious somewhere. Thanks in advance for enlightening me.
I don't think you're missing anything, I think the example is wrong. A working alternative to: boost::count_if(data, bll::_1 != 0) would be: boost::distance(data | filtered(bll::_1 != 0)) since boost::distance counts the total number of elements in the range. Regards, Nate