std::ranges::count, std::ranges::count_if
在標頭 <algorithm> 定義
|
||
調用簽名 |
||
(1) | ||
template< std::input_iterator I, std::sentinel_for<I> S, class T, class Proj = std::identity > |
(C++20 起) (C++26 前) |
|
template< std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, |
(C++26 起) | |
(2) | ||
template< ranges::input_range R, class T, class Proj = std::identity > requires std::indirect_binary_predicate |
(C++20 起) (C++26 前) |
|
template< ranges::input_range R, class Proj = std::identity, class T = std::projected_value_t<ranges::iterator_t<R>, Proj> > |
(C++26 起) | |
template< std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, |
(3) | (C++20 起) |
template< ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate< |
(4) | (C++20 起) |
返回範圍 [
first,
last)
中滿足特定判別標準的元素數。
此頁面上描述的函數式實體是算法函數對象(非正式地稱為 niebloid),即:
目錄 |
[編輯] 參數
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 << "数字:" << target1 << " 计数:" << num_items1 << '\n'; std::cout << "数字:" << target2 << " 计数:" << num_items2 << '\n'; // 用 lambda 表达式计数被 3 整除的元素。 int num_items3 = ranges::count_if(v.begin(), v.end(), [](int i){ return i % 3 == 0; }); std::cout << "可以被 3 整除的数字:" << num_items3 << '\n'; // 用 lambda 表达式计数被 11 整除的元素。 int num_items11 = ranges::count_if(v, [](int i){ return i % 11 == 0; }); std::cout << "可以被 11 整除的数字:" << 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); }
輸出:
数字:3 计数:2 数字:5 计数:0 可以被 3 整除的数字:3 可以被 11 整除的数字:0
[編輯] 參閱
(C++20) |
返回迭代器與哨位間的距離,或範圍起始與結尾間的距離 (算法函數對象) |
(C++20) |
從迭代器和計數創建子範圍 (定製點對象) |
由 range 中滿足某個謂詞的元素構成的 view (類模板) (範圍適配器對象) | |
返回滿足特定條件的元素數目 (函數模板) |