24 Algorithms library [algorithms]

24.5 Non-modifying sequence operations [alg.nonmodifying]

24.5.9 Count [alg.count]

template<class InputIterator, class T> constexpr typename iterator_traits<InputIterator>::difference_type count(InputIterator first, InputIterator last, const T& value); template<class ExecutionPolicy, class ForwardIterator, class T> typename iterator_traits<ForwardIterator>::difference_type count(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, const T& value); template<class InputIterator, class Predicate> constexpr typename iterator_traits<InputIterator>::difference_type count_if(InputIterator first, InputIterator last, Predicate pred); template<class ExecutionPolicy, class ForwardIterator, class Predicate> typename iterator_traits<ForwardIterator>::difference_type count_if(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, Predicate pred); namespace ranges { template<InputIterator I, Sentinel<I> S, class T, class Proj = identity> requires IndirectRelation<ranges::equal_to<>, projected<I, Proj>, const T*> constexpr iter_difference_t<I> count(I first, S last, const T& value, Proj proj = {}); template<InputRange R, class T, class Proj = identity> requires IndirectRelation<ranges::equal_to<>, projected<iterator_t<R>, Proj>, const T*> constexpr iter_difference_t<iterator_t<R>> count(R&& r, const T& value, Proj proj = {}); template<InputIterator I, Sentinel<I> S, class Proj = identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> constexpr iter_difference_t<I> count_if(I first, S last, Pred pred, Proj proj = {}); template<InputRange R, class Proj = identity, IndirectUnaryPredicate<projected<iterator_t<R>, Proj>> Pred> constexpr iter_difference_t<iterator_t<R>> count_if(R&& r, Pred pred, Proj proj = {}); }
Let E be:
  • *i == value for the overloads with no parameter pred or proj,
  • pred(*i) != false for the overloads with a parameter pred but no parameter proj,
  • invoke(proj, *i) == value for the overloads with a parameter proj but no parameter pred,
  • invoke(pred, invoke(proj, *i)) != false for the overloads with both parameters proj and pred.
Effects: Returns the number of iterators i in the range [first, last) for which E holds.
Complexity: Exactly last - first applications of the corresponding predicate and any projection.