std::experi...s::count_if(3)
std::experi...s::count_if(3) π
okay so welcome to the C++ Standard Libary manual page β yes, that spelling is exactly what the page says, no cap β for std::experimental::ranges::count and std::experimental::ranges::count_if. We are in ranges TS territory, and it is giving algorithm besties. π«Ά
πΈ NAME πΈ
std::experimental::ranges::count,std::experimental::ranges::count_if β the count squad, literally. π
β¨ Synopsis β¨
Defined in header <experimental/ranges/algorithm> β very demure, very ranges. β¨
Defined in header <experimental/ranges/algorithm>
template< InputIterator I, Sentinel<I> S, class T, class Proj =
ranges::identity >
requires IndirectRelation<ranges::equal_to<>, projected<I,
(1) (ranges TS)
Proj>, const T*>
ranges::difference_type_t<I> count( I first, S last, const T& value,
Proj proj = Proj{} );
template< InputRange R, class T, class Proj = ranges::identity >
requires IndirectRelation<ranges::equal_to<>,
projected<ranges::iterator_t<R>,
(2) (ranges TS)
Proj>, const T*>
ranges::difference_type_t<ranges::iterator_t<R>>
count( R&& r, const T& value, Proj proj = Proj{} );
template< InputIterator I, Sentinel<I> S, class Proj =
ranges::identity,
IndirectUnaryPredicate<projected<I, Proj>> Pred >
(3) (ranges TS)
ranges::difference_type_t<I> count_if( I first, S last, Pred pred,
Proj proj = Proj{} );
template< InputRange R, class Proj = ranges::identity,
IndirectUnaryPredicate<projected<ranges::iterator_t<R>,
Proj>> Pred >
(4) (ranges TS)
ranges::difference_type_t<ranges::iterator_t<R>>
count_if( R&& r, Pred pred, Proj proj = Proj{} );
Returns the number of elements in the range [first, last) satisfying specific criteria. That's the whole vibe. π
1)Counts the elements whose projected values are equal to value (i.e.,ranges::invoke(proj, *i) == value).3)Counts the elements whose projected values satisfy the predicate pred (i.e.,ranges::invoke(pred, ranges::invoke(proj, *i)) != false).2,4)Same as(1,3), but uses r as the source range, as if usingranges::begin(r)as first andranges::end(r)as last.
π¨ real-talk warning, bestie: Notwithstanding the declarations depicted above, the actual number and order of template parameters for algorithm declarations is unspecified. Thus, if explicit template arguments are used when calling an algorithm, the program is probably non-portable.
π Parameters π
first,lastβ the range of elements to examinerβ the range of elements to examinevalueβ the value to search forpredβ predicate to apply to the projected elementsprojβ projection to apply to the elements
π Return value π
Number of elements satisfying the condition. that's it, that's the return value. π
π¦ Complexity π¦
Exactly last - first comparisons / applications of the predicate, and the same number of applications of the projection. that's the exact budget, bestie. π¦
π Notes π
For the number of elements in the range [first, last) without any additional criteria, see ranges::distance. π«Ά
π Possible implementation π
okay so here's the under-the-hood look, copied exact so we don't lose a single character. π
First version
template<InputIterator I, Sentinel<I> S, class T, class Proj =
ranges::identity>
requires IndirectRelation<ranges::equal_to<>, projected<I, Proj>,
const T*>
ranges::difference_type_t<I> count(I first, S last, const T& value,
Proj proj = Proj{})
{
ranges::difference_type_t<I> ret = 0;
for (; first != last; ++first)
if (ranges::invoke(proj, *first) == value)
++ret;
return ret;
}
Second version
template<InputIterator I, Sentinel<I> S, class Proj =
ranges::identity,
IndirectUnaryPredicate<projected<I, Proj>> Pred>
ranges::difference_type_t<I> count_if(I first, S last, Pred pred,
Proj proj = Proj{})
{
ranges::difference_type_t<I> ret = 0;
for (; first != last; ++first)
if (ranges::invoke(pred, ranges::invoke(proj, *i)))
++ret;
return ret;
}
π Example π
This section is incomplete Reason: no example
π See also π
count,count_if(function template) β returns the number of elements satisfying specific criteriadistance(function template) β returns the distance between an iterator and a sentinel, or between the beginning and the end of a range
β Category β
Todo no example
okay bye bestie! π This page's footer energy is from http://cppreference.com, dated 2024.06.10, with the label std::experi...s::count_if(3). go count something and slay. β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms