Library: Algorithms
Function
Algorithm that counts the number of elements in a range that satisfy a given condition
#include <algorithm> namespace std { template<class InputIterator, class T> typename iterator_traits<InputIterator>::difference_type count(InputIterator start, InputIterator finish, const T& value); template <class InputIterator, class T, class Size> void count(InputIterator start, InputIterator finish, const T& value, Size& n); template<class InputIterator, class Predicate> typename iterator_traits<InputIterator>::difference_type count_if(InputIterator start, InputIterator finish, Predicate pred); template <class InputIterator, class Predicate, class Size> void count_if(InputIterator start, InputIterator finish, Predicate pred, Size& n); }
NOTE -- The second versions of the count() and count_if() functions are not part of the C++ Standard, but are included here as extensions for compatibility purposes. See Appendix B for a complete list of extensions of this implementation.
The count() algorithm compares value to elements in the sequence defined by iterators start and finish. The first version of count() returns the number of matches. The second version, which is provided for backwards compatibility or as an alternative to the first in case the template iterator_traits isn't provided, increments a counting value n each time it finds a match. In other words, count() returns (or adds to n) the number of iterators i in the range [start, finish) for which the following condition holds:
*i == value
Type T must be EqualityComparable.
The count_if() algorithm lets you specify a predicate, and returns the number of times an element in the sequence satisfies the predicate (or increments n that number of times). That is, count_if() returns (or adds to n) the number of iterators i in the range [start, finish) for which the following condition holds:
pred(*i) == true.
Both count() and count_if() perform exactly finish - start applications of the corresponding predicate.
// // count.cpp // #include <algorithm> // for count, count_if #include <functional> // bind2nd, less #include <iostream> // for cout, endl #include <vector> // for vector int main () { // Typedef for convenience. typedef std::vector<short, std::allocator<short> > vector; const vector::value_type a [] = { 1, 2, 3, 4, 5, 5, 7, 8, 9, 10 }; vector::size_type i = 0, j = 0, k = 0; // Set up a vector. const vector v (a + 0, a + 10); std::count (v.begin (), v.end (), 5, i); // Count fives std::count (v.begin (), v.end (), 6, j); // Count sixes // Count all elements less than 8. std::count_if (v.begin(), v.end(), std::bind2nd(std::less<vector::value_type>(), 8), k); std::cout << i << " " << j << " " << k << std::endl; return 0; } Program Output:
2 0 7
If your compiler does not support partial specialization, the first version of both count() and count_if(), the version that returns the count, is not available.
ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25.1.6