 st8b7c:f320:99b9:690f:4595:cd17:293a:c069count, st8b7c:f320:99b9:690f:4595:cd17:293a:c069count_if    Defined in header <algorithm>         (1)     template< class InputIt, class T >
typename iterator_traits<InputIt>8b7c:f320:99b9:690f:4595:cd17:293a:c069ifference_type
    count( InputIt first, InputIt last, const T& value );
  (until C++20)     template< class InputIt, class T >
constexpr typename iterator_traits<InputIt>8b7c:f320:99b9:690f:4595:cd17:293a:c069ifference_type
    count( InputIt first, InputIt last, const T& value );
  (since C++20)     template< class ExecutionPolicy, class ForwardIt, class T >
typename iterator_traits<ForwardIt>8b7c:f320:99b9:690f:4595:cd17:293a:c069ifference_type
    count( ExecutionPolicy&& policy,
           ForwardIt first, ForwardIt last, const T& value );
  (2)   (since C++17)     (3)     template< class InputIt, class UnaryPredicate >
typename iterator_traits<InputIt>8b7c:f320:99b9:690f:4595:cd17:293a:c069ifference_type
    count_if( InputIt first, InputIt last, UnaryPredicate p );
  (until C++20)     template< class InputIt, class UnaryPredicate >
constexpr typename iterator_traits<InputIt>8b7c:f320:99b9:690f:4595:cd17:293a:c069ifference_type
    count_if( InputIt first, InputIt last, UnaryPredicate p );
  (since C++20)     template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
typename iterator_traits<ForwardIt>8b7c:f320:99b9:690f:4595:cd17:293a:c069ifference_type
    count_if( ExecutionPolicy&& policy,
              ForwardIt first, ForwardIt last, UnaryPredicate p );
  (4)   (since C++17)    Returns the number of elements in the range [first, last) satisfying specific criteria.

1) counts the elements that are equal to value. 
3) counts elements for which predicate p returns true. 
2,4) Same as (1,3), but executed according to policy. These overloads do not participate in overload resolution unless  st8b7c:f320:99b9:690f:4595:cd17:293a:c069is_execution_policy_v<st8b7c:f320:99b9:690f:4595:cd17:293a:c069decay_t<ExecutionPolicy>>  (until C++20)  st8b7c:f320:99b9:690f:4595:cd17:293a:c069is_execution_policy_v<st8b7c:f320:99b9:690f:4595:cd17:293a:c069remove_cvref_t<ExecutionPolicy>>  (since C++20) is true.  Parameters    first, last   -   the range of elements to examine 
   value   -   the value to search for 
   policy   -   the execution policy to use. See execution policy for details. 
   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 InputIt, 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). ​

   Type requirements 
   -InputIt must meet the requirements of LegacyInputIterator. 
   -ForwardIt must meet the requirements of LegacyForwardIterator. 

  Return value number of elements satisfying the condition.
 Complexity exactly last - first comparisons / applications of the predicate.
 Exceptions The overloads with a template parameter named ExecutionPolicy report errors as follows:

 If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the standard policies, st8b7c:f320:99b9:690f:4595:cd17:293a:c069terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined. 
 If the algorithm fails to allocate memory, st8b7c:f320:99b9:690f:4595:cd17:293a:c069bad_alloc is thrown. 
  Notes For the number of elements in the range [first, last) without any additional criteria, see st8b7c:f320:99b9:690f:4595:cd17:293a:c069distance.
 Possible implementation See also the implementations of count in libstdc++ and libc++.
See also the implementations of count_if in libstdc++ and libc++.
   First version 
   template<class InputIt, class T>
typename iterator_traits<InputIt>8b7c:f320:99b9:690f:4595:cd17:293a:c069ifference_type
    count(InputIt first, InputIt last, const T& value)
{
    typename iterator_traits<InputIt>8b7c:f320:99b9:690f:4595:cd17:293a:c069ifference_type ret = 0;
    for (; first != last; ++first) {
        if (*first == value) {
            ret++;
        }
    }
    return ret;
} 
   Second version 
   template<class InputIt, class UnaryPredicate>
typename iterator_traits<InputIt>8b7c:f320:99b9:690f:4595:cd17:293a:c069ifference_type
    count_if(InputIt first, InputIt last, UnaryPredicate p)
{
    typename iterator_traits<InputIt>8b7c:f320:99b9:690f:4595:cd17:293a:c069ifference_type ret = 0;
    for (; first != last; ++first) {
        if (p(*first)) {
            ret++;
        }
    }
    return ret;
} 

  Example  #include <algorithm>
#include <iostream>
#include <iterator>
#include <array>
 
int main()
{
    constexpr st8b7c:f320:99b9:690f:4595:cd17:293a:c069array v = { 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069cout << "v: ";
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069copy(v.cbegin(), v.cend(), st8b7c:f320:99b9:690f:4595:cd17:293a:c069ostream_iterator<int>(st8b7c:f320:99b9:690f:4595:cd17:293a:c069cout, " "));
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069cout << '\n';
 
    // determine how many integers match a target value.
    for (const int target: {3, 4, 5}) {
        const int num_items = st8b7c:f320:99b9:690f:4595:cd17:293a:c069count(v.cbegin(), v.cend(), target);
        st8b7c:f320:99b9:690f:4595:cd17:293a:c069cout << "number: " << target << ", count: " << num_items << '\n';
    }
 
    // use a lambda expression to count elements divisible by 4.
    int count_div4 = st8b7c:f320:99b9:690f:4595:cd17:293a:c069count_if(v.begin(), v.end(), [](int i){return i % 4 == 0;});
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069cout << "numbers divisible by four: " << count_div4 << '\n';
 
    // A simplified version of `distance` with O(N) complexity:
    auto distance = [](auto first, auto last) {
        return st8b7c:f320:99b9:690f:4595:cd17:293a:c069count_if(first, last, [](auto){return true;});
    };
    static_assert(distance(v.begin(), v.end()) == 10);
} Output:
v: (980)406-9288 
number: 3, count: 2
number: 4, count: 2
number: 5, count: 0
numbers divisible by four: 3   See also     distance   returns the distance between two iterators  (function template) 
    ranges8b7c:f320:99b9:690f:4595:cd17:293a:c069ountranges8b7c:f320:99b9:690f:4595:cd17:293a:c069ount_if
(C++20)(C++20)   returns the number of elements satisfying specific criteria  (niebloid) 
  

    
{'path': 'public/docs/cpp/algorithm/count.html', 'source': 'DevDocs.IO'}