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)に含まれる要素のうち、特定の条件を満たすものの数を返します。
このページで説明されている関数のようなエンティティは、アルゴリズム関数オブジェクト(非公式にはニーブロイドとして知られている)です。つまり、
- これらのいずれかを呼び出す際に、明示的なテンプレート引数リストを指定することはできません。
- これらのいずれも実引数依存の名前探索には見えません。
- これらのいずれかが関数呼び出し演算子の左側の名前として通常の非修飾名探索によって見つかった場合、実引数依存の名前探索は抑制されます。
目次 |
[編集] パラメータ
| 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; // determine how many integers in a std::vector match a target value. 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 << "number: " << target1 << " count: " << num_items1 << '\n'; std::cout << "number: " << target2 << " count: " << num_items2 << '\n'; // use a lambda expression to count elements divisible by 3. int num_items3 = ranges::count_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 elements divisible by 11. int num_items11 = ranges::count_if(v, [](int i){ return i % 11 == 0; }); std::cout << "number divisible by eleven: " << 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); }
出力
number: 3 count: 2 number: 5 count: 0 number divisible by three: 3 number divisible by eleven: 0
[編集] 関連項目
| (C++20) |
イテレータと番兵 (sentinel) の間の距離、またはRangeの始点と終点の間の距離を返す (アルゴリズム関数オブジェクト) |
| (C++20) |
イテレータとカウントから部分rangeを作成する (カスタマイゼーションポイントオブジェクト) |
述語を満たすrangeの要素からなる view(クラステンプレート) (rangeアダプタオブジェクト) | |
| 特定の基準を満たす要素の数を返す (関数テンプレート) |