Boost logo

Boost :

Subject: Re: [boost] [Review:Algorithms] all.hpp vs std::count
From: Dave Abrahams (dave_at_[hidden])
Date: 2011-10-04 19:18:37


on Tue Oct 04 2011, Marshall Clow <mclow.lists-AT-gmail.com> wrote:

> On Oct 4, 2011, at 3:42 PM, Andrew Hundt wrote:
>
>> What is the advantage of the functions in all.hpp over std::count?
>>
>> I feel like one could simply call (in pseudocode) std::count == 0,
>> std::count == 1, std::count == class.size() to achieve the same effect.
>
> Clarity of naming, for one.
>
> if ( all_of ( sequence, predicate ))
> is a lot clearer than:
> if ( count_if ( sequence, value ) == sequence.count ())
>
> Performance, for another:
> all_of ( sequence, predicate ) only examines members of the sequence until one fails.
> count_if ( sequence, value ) examines every element of the sequence.

Furthermore, the size() operation on std::list is not (necessarily)
O(1).

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk