std::ranges::count_if() 算法
- 自 C++20 起
- 简化
- 详细
// (1)
constexpr std::iter_difference_t<I>
count_if( I first, S last, Pred pred, Proj proj = {} );
// (2)
constexpr ranges::range_difference_t<R>
count_if( R&& r, Pred pred, Proj proj = {} );
参数类型是泛型的,并具有以下约束
I
-std::input_iterator
S
-std::sentinel_for<I>
R
-std::ranges::input_range
Pred
:- (1) -
std::indirect_unary_predicate<std::projected<I, Proj>>
- (2) -
std::projected<ranges::iterator_t<R>, Proj>>
- (1) -
P
- (无)
对于所有重载,Proj
模板参数的默认类型为 std::identity
。
// (1)
template<
std::input_iterator I,
std::sentinel_for<I> S,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred
class Proj = std::identity
>
constexpr std::iter_difference_t<I>
count_if( I first, S last, Pred pred, Proj proj = {} );
// (2)
template<
ranges::input_range R,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred
class Proj = std::identity
>
constexpr ranges::range_difference_t<R>
count_if( R&& r, Pred pred, Proj proj = {} );
返回给定范围内满足特定条件的元素数量。
- (1) 计算谓词
p
返回true
的元素。 - (2) 与 (1) 相同,但使用
r
作为源范围,如同使用ranges::begin(r)
作为first
和ranges::end(r)
作为last
。
本页描述的函数类实体是niebloids。
参数
first last | 要检查的元素范围。 |
r | 要检查的元素范围。 |
pred | 应用于投影元素的谓词。 |
proj | 应用于元素的投影。 |
返回值
- (1 - 2) 谓词
p
为true
的元素数量。
复杂度
给定 N
为 ranges::distance(first, last)
- (1 - 2)
p
和投影的调用次数恰好为N
。
异常
(无)
可能的实现
ranges::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));
}
};
inline constexpr count_if_fn count_if;
备注
如果您想获取范围 [first
; last
) 或 r
中元素的数量,且没有任何额外条件,请使用 ranges::distance
。
示例
Main.cpp
#include <algorithm>
#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_if(v.begin(), v.end(), target1);
int num_items2 = ranges::count_if(v, target2);
std::cout << "number: " << target1 << " count_if: " << num_items1 << '\n';
std::cout << "number: " << target2 << " count_if: " << num_items2 << '\n';
// use a lambda expression to count_if elements divisible by 3.
int num_items3 = ranges::count_if_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_if elements divisible by 11.
int num_items11 = ranges::count_if_if(v, [](int i) {return i % 11 == 0;});
std::cout << "number divisible by eleven: " << num_items11 << '\n';
}
输出
number: 3 count_if: 2
number: 5 count_if: 0
number divisible by three: 3
number divisible by eleven: 0