Namespaces
Variants
Views
Actions

std::count, std::count_if

From cppreference.com
< cpp‎ | algorithm
Revision as of 16:08, 6 May 2011 by WikiSysop (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Template:cpp/algorithm/sidebar Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <algorithm>
</td>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td >
template< class InputIterator, class T >

typename iterator_traits<InputIterator>::difference_type

    count( InputIterator first, InputIterator last, const T &value );
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class InputIterator, class UnaryPredicate >

typename iterator_traits<InputIterator>::difference_type

    count_if( InputIterator first, InputIterator last, UnaryPredicate p );
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Returns the number of elements in the range [first, last) satisfying specific criteria. The first version counts the elements that are equal to value, the second version counts elements for which predicate p returns Template:cpp.

Template:params

first, last - the range of elements to examine
value - the value to search for
p - unary predicate which returns ​true for the required elements.

The expression p(v) must be convertible to bool for every argument v of type (possibly const) VT, where VT is the value type of InputIterator, regardless of value category, and must not modify v. Thus, a parameter type of VT&is not allowed, nor is VT unless for VT a move is equivalent to a copy(since C++11). ​

Template:returns number of elements satisfying the condition.

Template:complex

linear in the distance between first and last

Template:eq fun cpp Template:example cpp