std::count, std::count_if
定義於標頭檔案 <algorithm> |
||
(1) | ||
template< class InputIt, class T > typename std::iterator_traits<InputIt>::difference_type |
(C++20 起為 constexpr) (直到 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) | (C++20 起為 constexpr) |
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>> 為 true。 |
(C++20 前) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 為 true。 |
(C++20 起) |
目錄 |
[edit] 引數
first, last | - | 定義要檢查的元素範圍的迭代器對 |
value | - | 要搜尋的值 |
policy | - | 要使用的 執行策略 |
p | - | 一元謂詞,對於符合要求的元素返回true。 對於型別為(可能為 const) |
型別要求 | ||
-InputIt 必須滿足 LegacyInputIterator 的要求。 | ||
-ForwardIt 必須滿足 LegacyForwardIterator 的要求。 | ||
-UnaryPred 必須滿足 Predicate 的要求。 |
[edit] 返回值
範圍[
first,
last)
中滿足以下條件的迭代器 it 的數量:
[edit] 複雜度
給定 N 作為 std::distance(first, last)
[edit] 異常
帶有模板引數 ExecutionPolicy
的過載按如下方式報告錯誤
- 如果作為演算法一部分呼叫的函式執行時丟擲異常,並且
ExecutionPolicy
是標準策略之一,則呼叫std::terminate。對於任何其他ExecutionPolicy
,行為是實現定義的。 - 如果演算法未能分配記憶體,則丟擲 std::bad_alloc。
[edit] 注意
有關範圍 [
first,
last)
中不帶任何附加條件的元素數量,請參閱 std::distance。
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_algorithm_default_value_type |
202403 |
(C++26) | 演算法的列表初始化 (1,2) |
[edit] 可能實現
另請參閱 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; } |
[edit] 示例
#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
[edit] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 283 | C++98 | 要求 T 為 EqualityComparable,但InputIt 的值型別並不總是 T |
移除了此要求 |
[edit] 另請參閱
返回兩個迭代器間的距離 (函式模板) | |
(C++20)(C++20) |
返回滿足特定條件的元素數量 (演算法函式物件) |