std::count, std::count_if
定义于头文件 <algorithm> |
||
(1) | ||
template< class InputIt, class T > typename std::iterator_traits<InputIt>::difference_type |
(constexpr 自 C++20 起) (直到 C++26) |
|
template< class InputIt, class T = typename std::iterator_traits <InputIt>::value_type > |
(自 C++26 起) | |
(2) | ||
template< class ExecutionPolicy, class ForwardIt, class T > typename std::iterator_traits<ForwardIt>::difference_type |
(自 C++17 起) (直到 C++26) |
|
template< class ExecutionPolicy, class ForwardIt, class T = typename std::iterator_traits |
(自 C++26 起) | |
template< class InputIt, class UnaryPred > typename std::iterator_traits<InputIt>::difference_type |
(3) | (constexpr 自 C++20 起) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred > typename std::iterator_traits<ForwardIt>::difference_type |
(4) | (自 C++17 起) |
返回范围 [first, last) 中满足特定条件的元素数量。
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true. |
(直到 C++20) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true. |
(自 C++20 起) |
目录 |
[编辑] 参数
first, last | - | 定义要检查的元素范围的迭代器对 |
value | - | 要搜索的值 |
policy | - | 要使用的执行策略 |
p | - | 一元谓词,对于所需的元素返回 true。 对于 InputIt 的值类型 VT 的每个参数 v(可能是 const),表达式 p(v) 必须可转换为 bool,无论值类别如何,并且不得修改 v。因此,不允许使用 VT& 类型的参数,除非对于 |
类型要求 | ||
-InputIt 必须满足 LegacyInputIterator 的要求。 | ||
-ForwardIt 必须满足 LegacyForwardIterator 的要求。 | ||
-UnaryPred 必须满足 Predicate 的要求。 |
[编辑] 返回值
范围 [first, last) 中满足以下条件的迭代器 it 的数量
[编辑] 复杂度
给定 N 为 std::distance(first, last)
[编辑] 异常
具有名为 ExecutionPolicy
的模板参数的重载按如下方式报告错误
- 如果作为算法一部分调用的函数的执行抛出异常,并且
ExecutionPolicy
是 标准策略 之一,则调用 std::terminate。对于任何其他ExecutionPolicy
,行为是实现定义的。 - 如果算法无法分配内存,则抛出 std::bad_alloc。
[编辑] 注释
对于范围 [first, last) 中没有附加条件的元素数量,请参阅 std::distance。
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_algorithm_default_value_type |
202403 |
(C++26) | 列表初始化 用于算法 (1,2) |
[编辑] 可能的实现
另请参阅 libstdc++ 和 libc++ 中的 count
实现。
另请参阅 libstdc++ 和 libc++ 中的 count_if
实现。
count (1) |
---|
template<class InputIt, class T = typename std::iterator_traits<InputIt>::value_type> typename std::iterator_traits<InputIt>::difference_type count(InputIt first, InputIt last, const T& value) { typename std::iterator_traits<InputIt>::difference_type ret = 0; for (; first != last; ++first) if (*first == value) ++ret; return ret; } |
count_if (3) |
template<class InputIt, class UnaryPred> typename std::iterator_traits<InputIt>::difference_type count_if(InputIt first, InputIt last, UnaryPred p) { typename std::iterator_traits<InputIt>::difference_type ret = 0; for (; first != last; ++first) if (p(*first)) ++ret; return ret; } |
[编辑] 示例
#include <algorithm> #include <array> #include <cassert> #include <complex> #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); std::array<std::complex<double>, 3> nums{{{4, 2}, {1, 3}, {4, 2}}}; #ifdef __cpp_lib_algorithm_default_value_type // T gets deduced making list-initialization possible auto c = std::count(nums.cbegin(), nums.cend(), {4, 2}); #else auto c = std::count(nums.cbegin(), nums.cend(), std::complex<double>{4, 2}); #endif assert(c == 2); }
输出
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
[编辑] 缺陷报告
以下行为变更缺陷报告已追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 283 | C++98 | T 被要求是 EqualityComparable,但 InputIt 的值类型并不总是 T |
移除了该要求 |
[编辑] 参见
返回两个迭代器之间的距离 (函数模板) | |
(C++20)(C++20) |
返回满足特定条件的元素数量 (算法函数对象) |