[range] Best way to count the elements in a filtered range?
I wonder what is the recommended way to count the elements in a filtered range. Let range be a random access range, and filter the result of filtered(predicate), auto filtered_range = range | filter; count_if( filtered_range, [](const&){ return true; }); // (& omitted in lambda) counts the elements in a filtered range. _If_ this is the best way to count the elements in a filtered range, what about defining Range boost::count(Range&& r); to count all element in the range? It could call size() if available and count_if otherwise. Bests, Gonzalo BG
On 19 June 2013 11:41, Gonzalo BG
I wonder what is the recommended way to count the elements in a filtered range.
Let range be a random access range, and filter the result of filtered(predicate),
auto filtered_range = range | filter; count_if( filtered_range, [](const&){ return true; }); // (& omitted in lambda)
I think boost::size() is what you want. size_t s = boost::size(range | filter); See: http://www.boost.org/doc/libs/1_53_0/boost/range/size.hpp -- Darren
Sorry, the email was send twice. Please refer to the _other_ thread.
P.S: can someone delete this thread?
On Wed, Jun 19, 2013 at 1:06 PM, Darren Garvey
On 19 June 2013 11:41, Gonzalo BG
wrote: I wonder what is the recommended way to count the elements in a filtered range.
Let range be a random access range, and filter the result of filtered(predicate),
auto filtered_range = range | filter; count_if( filtered_range, [](const&){ return true; }); // (& omitted in lambda)
I think boost::size() is what you want.
size_t s = boost::size(range | filter);
See: http://www.boost.org/doc/libs/1_53_0/boost/range/size.hpp
-- Darren
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
participants (3)
-
Darren Garvey
-
Gonzalo BG
-
Gonzalo Brito Gadeschi