std::ranges...s::count_if(3)
π std::ranges...s::count_if(3) π
Okay bestie, welcome to the C++ Standard Libary manual page for std::ranges...s::count_if(3) β yes, that's the title and that's the manual name. This page is all about the std::ranges::count and std::ranges::count_if niebloids, and honestly they ate. Let's get into it π
πΈ NAME πΈ
std::ranges::count,std::ranges::count_if β std::ranges::count,std::ranges::count_if
β¨ Synopsis β¨
Defined in header <algorithm>
Call signature
// count (1) β since C++20, until C++26
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 = {} );
// count (1) β since 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 = {} );
// count (2) β since C++20, until 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 = {} );
// count (2) β since 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 = {} );
// count_if (3) β since C++20
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 = {} );
// count_if (4) β 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 = {} );
Returns the number of elements in the range [first, last) satisfying specific criteria.
- Counts the elements that are equal to
value. - Counts elements for which predicate
preturnstrue. 2,4. Same as (1,3), but usesras the source range, as if usingranges::begin(r)asfirstandranges::end(r)aslast.
The function-like entities described on this page are niebloids, that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
In practice, they may be implemented as function objects, or with special compiler extensions.
π Parameters π
first,lastβ the range of elements to examine.rβ the range of the elements to examine.valueβ the value to search for.predβ predicate to apply to the projected elements.projβ projection to apply to the elements.
π Return value π
Number of elements satisfying the condition.
π Complexity π
Exactly last - first comparisons and projection.
π§ Notes π§
For the number of elements in the range without any additional criteria, see std::ranges::distance.
π§ͺ Feature-test macro π§ͺ
__cpp_lib_algorithm_default_value_typeβ value202403β Std(C++26)β feature: List-initialization for algorithms(1,2)
π Possible implementation π
count (1)
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));
}
};
inline constexpr count_fn count;
count_if (3)
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));
}
};
inline constexpr count_if_fn count_if;
π Example π
// Run this code
#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 π¦
ranges::distance(C++20) β returns the distance between an iterator and a sentinel, or between the beginning and end of a range (niebloid)views::counted(C++20) β creates a subrange from an iterator and a count (customization point object)ranges::filter_view,views::filter(C++20) β a view that consists of the elements of a range that satisfies a predicate (class template) (range adaptor object)count,count_ifβ returns the number of elements satisfying specific criteria (function template)
okay that's the whole page, bestie π β source: http://cppreference.com, version/date: 2024.06.10, page: std::ranges...s::count_if(3). Mwah π
π¦ 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