std::ranges::count, std::ranges::count_if
来自 zh.cppreference.net
| 定义于头文件 <algorithm>
|
||
| 调用签名 |
||
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) | (自 C++20) (直至 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 = {} );
|
(自 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) | (自 C++20) (直至 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 = {} );
|
(自 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) | (自 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) | (自 C++20) |
返回范围 [first, last) 中满足特定条件的元素数量。
1) 计算等于
value 的元素数量。3) 计算谓词
p 返回 true 的元素数量。2,4) 与 (1,3) 相同,但使用
r 作为源范围,就好像使用 ranges::begin(r) 作为 first 并且使用 ranges::end(r) 作为 last 一样。本页描述的函数式实体是算法函数对象 (非正式地称为niebloids),即:
参数
| first, last | - | 定义待检查元素范围的 迭代器-哨位 对 |
| r | - | 待检查元素的范围 |
| value | - | 要搜索的值 |
| pred | - | 应用于投影元素的谓词 |
| proj | - | 应用于元素的投影 |
返回值
满足条件的元素数量。
复杂度
恰好进行 last - first 次比较和投影操作。
注释
对于不包含任何附加条件的范围内元素数量,请参阅 std::ranges::distance 。
| 功能测试 宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_algorithm_default_value_type
|
202403
|
(C++26) | 列表初始化 用于算法 ( 1,2 ) |
可能的实现
| 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; |
示例
运行此代码
#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; // 确定 std::vector 中有多少整数匹配目标值 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'; // 使用 lambda 表达式统计能被 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'; // 使用 lambda 表达式统计能被 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); }
输出:
number: 3 count: 2 number: 5 count: 0 number divisible by three: 3 number divisible by eleven: 0
参见
|
(C++20)
|
返回迭代器与哨位之间的距离,或范围的起始与末尾之间的距离
(算法函数对象) |
|
(C++20)
|
通过迭代器和计数创建子范围
(定制点对象) |
由满足谓词的
range
元素组成的
view
(类模板) (范围适配器对象) |
|
|
返回满足特定条件的元素数量
(函数模板) |