Declaration
template <class InputIterator, class T, class Size> void count(InputIterator first, InputIterator last, const T& value, Size &n); template <class InputIterator, class Predicate, class Size> void count_if(InputIterator first, InputIterator last, Predicate pred, Size &n);
Description The count function adds the number of elements in the range [ first, last) that are equal to value, placing the result into the reference argument n.
The count_if function adds the number of elements in the range [ first, last) on which the predicate pred returns true (i.e., a nonzero value), placing the result into the reference argument n.
In the first function checks for element equality are made with operator==, while in the second they are made with the function object pred.
Type requirements
Group Non-mutating sequence operations.
Time complexity Linear.
The number of equality operations performed, or of applications of the predicate pred, is the size of the range [ first, last) .
Space complexity Constant.
Mutative? No.
Glossary terms constant space, dereference type, function object, function object type, InputIterator, int*, iterator, iterator type, linear time, mutative, operator ==, Predicate, predicate, range, reference argument, sequence, size type.
Example Select here to view the source code for the example.
Implementation Select here to view source code (Copyright (C) 1994, Hewlett-Packard Company) for this algorithm.
Implementation notes count and count_if must store the result into a reference argument instead of returning the result because the size type cannot be deduced from the built-in iterator types such as int*.