std::count_if() 算法
- 自 C++20 起
- 自 C++17 起
- C++17 之前
// (1)
template< class InputIt, class UnaryPredicate >
constexpr typename iterator_traits<InputIt>::difference_type
count_if( InputIt first, InputIt last, UnaryPredicate p );
// (2)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
typename iterator_traits<ForwardIt>::difference_type
count_if( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, UnaryPredicate p );
// (1)
template< class InputIt, class UnaryPredicate >
typename iterator_traits<InputIt>::difference_type
count_if( InputIt first, InputIt last, UnaryPredicate p );
// (2)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
typename iterator_traits<ForwardIt>::difference_type
count_if( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, UnaryPredicate p );
// (1)
template< class InputIt, class UnaryPredicate >
typename iterator_traits<InputIt>::difference_type
count_if( InputIt first, InputIt last, UnaryPredicate p );
返回范围 [first1
; last1
) 中满足特定条件的元素数量。
- (1) 计算谓词
p
返回true
的元素。 - (2) 同 (1),但根据策略执行。
重载解析
这些重载仅在
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>
(C++20 之前)std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>
(C++20 起) 为true
时参与重载解析。
参数
first last | 要检查的元素范围。 |
policy | 要使用的执行策略。有关详细信息,请参阅执行策略。 |
count_if | 一元谓词,对所需元素返回 对于类型为 (可能是 const) |
类型要求
InputIt | LegacyInputIterator |
ForwardIt | LegacyForwardIterator |
返回值
- (1 - 2) 谓词
p
为true
的元素数量。
复杂度
给定 N
为 std::distance(first, last)
- (1 - 2)
p
的精确 N 次应用
异常
带有模板参数 ExecutionPolicy
的重载报告错误如下
- 如果作为算法一部分调用的函数执行时抛出异常且
ExecutionPolicy
是标准策略之一,则会调用std::terminate
。对于任何其他ExecutionPolicy
,行为是实现定义的. - 如果算法未能分配内存,则抛出
std::bad_alloc
。
可能的实现
count_if (1)
template<class InputIt, class UnaryPredicate>
typename iterator_traits<InputIt>::difference_type
count_if(InputIt first, InputIt last, UnaryPredicate p)
{
typename iterator_traits<InputIt>::difference_type ret = 0;
for (; first != last; ++first)
if (p(*first))
++ret;
return ret;
}
备注
如果要在范围 [first
; last
) 中获取元素数量而没有任何附加条件,请使用std::distance
示例
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
int main()
{
constexpr std::array v {1, 2, 3, 4, 4, 3, 7, 8, 9, 10};
std::cout << "v: ";
std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
// determine how many integers match a target value.
for (const int target: {3, 4, 5})
{
const int num_items = std::count(v.cbegin(), v.cend(), target);
std::cout << "number: " << target << ", count: " << num_items << '\n';
}
// use a lambda expression to count elements divisible by 4.
int count_div4 = std::count_if(v.begin(), v.end(), [](int i) { return i % 4 == 0; });
std::cout << "numbers divisible by four: " << count_div4 << '\n';
// A simplified version of `distance` with O(N) complexity:
auto distance = [](auto first, auto last)
{
return std::count_if(first, last, [](auto) { return true; });
};
static_assert(distance(v.begin(), v.end()) == 10);
}
v: 1 2 3 4 4 3 7 8 9 10
number: 3, count: 2
number: 4, count: 2
number: 5, count: 0
numbers divisible by four: 3