Namespaces
Variants

std::ranges::count, std::ranges::count_if

From cppreference.com
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Non-modifying sequence operations    
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
(C++11)    

Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations


 
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
       
       
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
       
       
Permutation operations
Fold operations
Numeric operations
(C++23)            
Operations on uninitialized storage
Return types
 
Defined in header <algorithm>
Call signature
template< std::input_iterator I, std::sentinel_for<I> S,
          class T, class Proj = std::identity >
    requires std::indirect_binary_predicate
                 <ranges::equal_to, std::projected<I, Proj>, const T*>
constexpr std::iter_difference_t<I>
    count( I first, S last, const T& value, Proj proj = {} );
(1) (since C++20)
(until C++26)
template< std::input_iterator I, std::sentinel_for<I> S,
          class Proj = std::identity,
          class T = std::projected_value_t<I, Proj> >
    requires std::indirect_binary_predicate
                 <ranges::equal_to, std::projected<I, Proj>, const T*>
constexpr std::iter_difference_t<I>
    count( I first, S last, const T& value, Proj proj = {} );
(since C++26)
template< ranges::input_range R, class T, class Proj = std::identity >
    requires std::indirect_binary_predicate
                 <ranges::equal_to,
                  std::projected<ranges::iterator_t<R>, Proj>, const T*>
constexpr ranges::range_difference_t<R>
    count( R&& r, const T& value, Proj proj = {} );
(2) (since C++20)
(until C++26)
template< ranges::input_range R, class Proj = std::identity,
          class T = std::projected_value_t<ranges::iterator_t<R>, Proj> >
    requires std::indirect_binary_predicate
                 <ranges::equal_to,
                  std::projected<ranges::iterator_t<R>, Proj>, const T*>
constexpr ranges::range_difference_t<R>
    count( R&& r, const T& value, Proj proj = {} );
(since C++26)
template< std::input_iterator I, std::sentinel_for<I> S,
          class Proj = std::identity,
          std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
constexpr std::iter_difference_t<I>
    count_if( I first, S last, Pred pred, Proj proj = {} );
(3) (since C++20)
template< ranges::input_range R, class Proj = std::identity,
          std::indirect_unary_predicate
              <std::projected<ranges::iterator_t<R>, Proj>> Pred >
constexpr ranges::range_difference_t<R>
    count_if( R&& r, Pred pred, Proj proj = {} );
(4) (since C++20)
template< /*execution-policy*/ Ep,
          std::random_access_iterator I, std::sized_sentinel_for<I> S,
          class Proj = std::identity,
          class T = std::projected_value_t<I, Proj> >
    requires std::indirect_binary_predicate
                 <ranges::equal_to, std::projected<I, Proj>, const T*>
std::iter_difference_t<I>
    count( Ep&& policy, I first, S last, const T& value, Proj proj = {} );
(5) (since C++26)
template< /*execution-policy*/ Ep, /*sized-random-access-range*/ R,
          class Proj = std::identity,
          class T = std::projected_value_t<ranges::iterator_t<R>, Proj> >
    requires std::indirect_binary_predicate
                 <ranges::equal_to,
                  std::projected<ranges::iterator_t<R>, Proj>, const T*>
ranges::range_difference_t<R>
    count( Ep&& policy, R&& r, const T& value, Proj proj = {} );
(6) (since C++26)
template< /*execution-policy*/ Ep,
          std::random_access_iterator I, std::sized_sentinel_for<I> S,
          class Proj = std::identity,
          std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
std::iter_difference_t<I>
    count_if( Ep&& policy, I first, S last, Pred pred, Proj proj = {} );
(7) (since C++26)
template< /*execution-policy*/ Ep, /*sized-random-access-range*/ R,
          class Proj = std::identity,
          std::indirect_unary_predicate
              <std::projected<ranges::iterator_t<R>, Proj>> Pred >
ranges::range_difference_t<R>
    count_if( Ep&& policy, R&& r, Pred pred, Proj proj = {} );
(8) (since C++26)

For the definition of /*execution-policy*/, see this page; for the definition of /*sized-random-access-range*/, see this page.

Returns the number of elements (projected by proj) in the source range satisfying specific criteria.

1,2) Counts the elements that are equal to value.
1) The source range is [firstlast).
2) The source range is r.
3,4) Counts elements for which predicate p returns true.
3) The source range is [firstlast).
4) The source range is r.
5-8) Same as (1-4), but executed according to policy.

The function-like entities described on this page are algorithm function objects (informally known as niebloids), that is:

Parameters

first, last - the iterator-sentinel pair defining the source range
r - the source range
value - the value to search for
pred - the predicate to be applied to the (projected) elements
proj - the projection to be applied to the elements
policy - the execution policy to use

Return value

The number of iterators iter in the source range satisfying the following condition:

1,2,5,6) std::invoke(proj, *iter) == value is true.
3,4,7,8) bool(std::invoke(pred, std::invoke(proj, *iter))) is true.

Complexity

Given N as ranges::distance(first, last) or ranges::distance(r):

1,2) Exactly N comparisons with value using operator==.
3,4) Exactly N applications of pred.
5,6) 𝓞(N) comparisons with value using operator==.
7,8) 𝓞(N) applications of pred.

Exceptions

5-8) During the execution process:
  • If the temporary memory resources required for parallelization are not available, std::bad_alloc is thrown.
  • If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for standard policies, std::terminate is invoked).

Notes

For the number of elements in the range without any additional criteria, see ranges::distance.

Feature-test macro Value Std Feature
__cpp_lib_algorithm_default_value_type 202403 (C++26) List-initialization for algorithms (1,2)

Possible implementation

count
struct count_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S,
             class Proj = std::identity, class T = std::projected_value_t<I, Proj>>
        requires std::indirect_binary_predicate<ranges::equal_to,
                                                std::projected<I, Proj>, const T*>
    constexpr std::iter_difference_t<I>
        operator()(I first, S last, const T& value, Proj proj = {}) const
    {
        std::iter_difference_t<I> counter = 0;
        for (; first != last; ++first)
            if (std::invoke(proj, *first) == value)
                ++counter;
        return counter;
    }
    
    template<ranges::input_range R, class Proj = std::identity
             class T = std::projected_value_t<ranges::iterator_t<R>, Proj>>
        requires std::indirect_binary_predicate
                     <ranges::equal_to,
                      std::projected<ranges::iterator_t<R>, Proj>, const T*>
    constexpr ranges::range_difference_t<R>
        operator()(R&& r, const T& value, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), value, std::ref(proj));
    }
    
    template<ranges::forward_range R, class Proj = std::identity
             class T = std::projected_value_t<ranges::iterator_t<R>, Proj>>
        requires std::indirect_binary_predicate
                     <ranges::equal_to,
                      std::projected<ranges::iterator_t<R>, Proj>, const T*>
    constexpr ranges::range_difference_t<R>
        operator()(R&& r, const T& value, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r),
                       ranges::next(ranges::begin(r), ranges::end(r)),
                       value, std::ref(proj));
    }
};

inline constexpr count_fn count;
count_if
struct count_if_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S,
             class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
    constexpr std::iter_difference_t<I>
        operator()(I first, S last, Pred pred, Proj proj = {}) const
    {
        std::iter_difference_t<I> counter = 0;
        for (; first != last; ++first)
            if (std::invoke(pred, std::invoke(proj, *first)))
                ++counter;
        return counter;
    }
    
    template<ranges::input_range R, class Proj = std::identity,
             std::indirect_unary_predicate
                 <std::projected<ranges::iterator_t<R>, Proj>> Pred>
    constexpr ranges::range_difference_t<R>
        operator()(R&& r, Pred pred, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r),
                       std::ref(pred), std::ref(proj));
    }
    
    template<ranges::forward_range R, class Proj = std::identity,
             std::indirect_unary_predicate
                 <std::projected<ranges::iterator_t<R>, Proj>> Pred>
    constexpr ranges::range_difference_t<R>
        operator()(R&& r, Pred pred, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r),
                       ranges::next(ranges::begin(r), ranges::end(r)),
                       std::ref(pred), std::ref(proj));
    }
};

inline constexpr count_if_fn count_if;

Example

#include <algorithm>
#include <cassert>
#include <complex>
#include <iostream>
#include <vector>
 
int main()
{
    std::vector<int> v{1, 2, 3, 4, 4, 3, 7, 8, 9, 10};
    
    namespace ranges = std::ranges;
    
    // determine how many integers in a std::vector match a target value.
    int target1 = 3;
    int target2 = 5;
    int num_items1 = ranges::count(v.begin(), v.end(), target1);
    int num_items2 = ranges::count(v, target2);
    std::cout << "number: " << target1 << " count: " << num_items1 << '\n';
    std::cout << "number: " << target2 << " count: " << num_items2 << '\n';
    
    // use a lambda expression to count elements divisible by 3.
    int num_items3 = ranges::count_if(v.begin(), v.end(), [](int i){ return i % 3 == 0; });
    std::cout << "number divisible by three: " << num_items3 << '\n';
    
    // use a lambda expression to count elements divisible by 11.
    int num_items11 = ranges::count_if(v, [](int i){ return i % 11 == 0; });
    std::cout << "number divisible by eleven: " << num_items11 << '\n';
    
    std::vector<std::complex<double>> nums{{4, 2}, {1, 3}, {4, 2}};
    #ifdef __cpp_lib_algorithm_default_value_type
        auto c = ranges::count(nums, {4, 2});
    #else
        auto c = ranges::count(nums, std::complex<double>{4, 2});
    #endif
    assert(c == 2);
}

Output:

number: 3 count: 2
number: 5 count: 0
number divisible by three: 3
number divisible by eleven: 0

See also

returns the number of elements satisfying specific criteria
(function template) [edit]
returns the distance between an iterator and a sentinel, or between the beginning and end of a range
(algorithm function object)[edit]
creates a subrange from an iterator and a count
(customization point object)[edit]
a view that consists of the elements of a range that satisfy a predicate
(class template) (range adaptor object)[edit]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.