std::ranges::all_of, std::ranges::any_of, std::ranges::none_of
From cppreference.com
| ヘッダー <algorithm> で定義 |
||
| 呼び出しシグネチャ |
||
template< std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, |
(1) | (C++20以降) |
| template< ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate< |
(2) | (C++20以降) |
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以降) |
template< std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, |
(5) | (C++20以降) |
| template< ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate< |
(6) | (C++20以降) |
1) 単項述語 `pred` が、射影 `proj` で射影された後、範囲 `[first, last)` 内の少なくとも一つの要素に対して `false` を返すかどうかをチェックします。
3) 単項述語 `pred` が、射影 `proj` で射影された後、範囲 `[first, last)` 内の少なくとも一つの要素に対して `true` を返すかどうかをチェックします。
5) 単項述語 `pred` が、射影 `proj` で射影された後、範囲 `[first, last)` 内のどの要素に対しても `true` を返さないかどうかをチェックします。
2,4,6) (1,3,5) と同じですが、r をソース範囲として使用します。これは、ranges::begin(r) を first として、ranges::end(r) を last として使用するかのようです。
このページで説明されている関数のようなエンティティは、アルゴリズム関数オブジェクト(非公式にはニーブロイドとして知られている)です。つまり、
- これらのいずれかを呼び出す際に、明示的なテンプレート引数リストを指定することはできません。
- これらのいずれも実引数依存の名前探索には見えません。
- これらのいずれかが関数呼び出し演算子の左側の名前として通常の非修飾名探索によって見つかった場合、実引数依存の名前探索は抑制されます。
目次 |
[edit] パラメータ
| first, last | - | 調査する要素の範囲を定義するイテレータとセンチネルのペア |
| r | - | 検査する要素の範囲 |
| pred | - | 射影された要素に適用する述語 |
| proj | - | 要素に適用する射影 |
[edit] 戻り値
1,2) 範囲内のすべてのイテレータ `i` に対して `std::invoke(pred, std::invoke(proj, *i)) != false` が成り立つ場合は `true`、そうでない場合は `false` を返します。範囲が空の場合は `true` を返します。
3,4) 範囲内の少なくとも一つのイテレータ `i` に対して `std::invoke(pred, std::invoke(proj, *i)) != false` が成り立つ場合は `true`、そうでない場合は `false` を返します。範囲が空の場合は `false` を返します。
5,6) 範囲内のすべてのイテレータ `i` に対して `std::invoke(pred, std::invoke(proj, *i)) == false` が成り立つ場合は `true`、そうでない場合は `false` を返します。範囲が空の場合は `true` を返します。
| 要素に `true` がある | はい | いいえ | ||
|---|---|---|---|---|
| 要素に `false` がある | はい | いいえ | はい | なし[1] |
all_of
|
false | true | false | true |
any_of
|
true | true | false | false |
none_of
|
false | false | true | true |
- ↑ この場合、範囲は空です。
[edit] 計算量
述語と射影の適用は最大で `last - first` 回です。
[edit] 実装例
| all_of (1,2) |
|---|
struct all_of_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 bool operator()(I first, S last, Pred pred, Proj proj = {}) const { return ranges::find_if_not(first, last, std::ref(pred), std::ref(proj)) == last; } template<ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate< std::projected<ranges::iterator_t<R>,Proj>> Pred> constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const { return operator()(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } }; inline constexpr all_of_fn all_of; |
| any_of (3,4) |
struct any_of_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 bool operator()(I first, S last, Pred pred, Proj proj = {}) const { return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) != last; } template<ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate< std::projected<ranges::iterator_t<R>,Proj>> Pred> constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const { return operator()(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } }; inline constexpr any_of_fn any_of; |
| none_of (5,6) |
struct none_of_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 bool operator()(I first, S last, Pred pred, Proj proj = {}) const { return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) == last; } template<ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate< std::projected<ranges::iterator_t<R>,Proj>> Pred> constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const { return operator()(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } }; inline constexpr none_of_fn none_of; |
[edit] 例
このコードを実行
#include <algorithm> #include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> namespace ranges = std::ranges; constexpr bool some_of(auto&& r, auto&& pred) // some but not all { return not (ranges::all_of(r, pred) or ranges::none_of(r, pred)); } constexpr auto w = {1, 2, 3}; static_assert(!some_of(w, [](int x) { return x < 1; })); static_assert( some_of(w, [](int x) { return x < 2; })); static_assert(!some_of(w, [](int x) { return x < 4; })); int main() { std::vector<int> v(10, 2); std::partial_sum(v.cbegin(), v.cend(), v.begin()); std::cout << "Among the numbers: "; ranges::copy(v, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; if (ranges::all_of(v.cbegin(), v.cend(), [](int i) { return i % 2 == 0; })) std::cout << "All numbers are even\n"; if (ranges::none_of(v, std::bind(std::modulus<int>(), std::placeholders::_1, 2))) std::cout << "None of them are odd\n"; auto DivisibleBy = [](int d) { return [d](int m) { return m % d == 0; }; }; if (ranges::any_of(v, DivisibleBy(7))) std::cout << "At least one number is divisible by 7\n"; }
出力
Among the numbers: 2 4 6 8 10 12 14 16 18 20 All numbers are even None of them are odd At least one number is divisible by 7
[edit] 関連項目
| (C++11)(C++11)(C++11) |
範囲内のすべての、いずれかの、またはどの要素も述語が true にならないかをチェックする (関数テンプレート) |