std::experimental::ranges::all_of、std::experimental::ranges::any_of、std::experimental::ranges::none_of
From cppreference.com
< cpp | experimental | ranges
| Defined in header <experimental/ranges/algorithm> |
||
| template< InputIterator I, Sentinel<I> S, class Proj = ranges::identity, IndirectUnaryPredicate<projected<I, Proj>> Pred > |
(1) | (ranges TS) |
| template< InputRange R, class Proj = ranges::identity, IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred > |
(2) | (ranges TS) |
| template< InputIterator I, Sentinel<I> S, class Proj = ranges::identity, IndirectUnaryPredicate<projected<I, Proj>> Pred > |
(3) | (ranges TS) |
| template< InputRange R, class Proj = ranges::identity, IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred > |
(4) | (ranges TS) |
| template< InputIterator I, Sentinel<I> S, class Proj = identity, IndirectUnaryPredicate<projected<I, Proj>> Pred > |
(5) | (ranges TS) |
| template< InputRange R, class Proj = ranges::identity, IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred > |
(6) | (ranges TS) |
1) 単項述語 pred が範囲
[first, last) のすべての要素に対して true を返すかどうかをチェックします。3) 単項述語 pred が範囲
[first, last) の少なくとも1つの要素に対して true を返すかどうかをチェックします。5) 単項述語 pred が範囲
[first, last) のどの要素に対しても true を返さないかどうかをチェックします。2,4,6) (1,3,5) と同じですが、r をソース範囲として使用します。これは、ranges::begin(r) を first として、ranges::end(r) を last として使用するかのようです。
上記に示された宣言にもかかわらず、アルゴリズム宣言のテンプレートパラメータの実際の数と順序は未指定です。したがって、アルゴリズムを呼び出す際に明示的なテンプレート引数を使用すると、プログラムはおそらくポータブルではありません。
目次 |
[編集] Parameters
| first, last | - | 検査する要素の範囲 |
| r | - | 検査する要素の範囲 |
| pred | - | 射影された要素に適用する述語 |
| proj | - | 要素に適用する射影 |
[編集] Return value
1,2) true は、pred が範囲内のすべての要素に対して true を返す場合に返されます。それ以外の場合は false です。範囲が空の場合は true を返します。
3,4) true は、pred が範囲内の少なくとも1つの要素に対して true を返す場合に返されます。それ以外の場合は false です。範囲が空の場合は false を返します。
5,6) true は、pred が範囲内のどの要素に対しても true を返さない場合に返されます。それ以外の場合は false です。範囲が空の場合は true を返します。
[編集] Complexity
1-6) 述語の適用は最大 last - first 回、射影の適用は最大 last - first 回です。
[編集] Possible implementation
| 最初のバージョン |
|---|
template<InputIterator I, Sentinel<I> S, class Proj = ranges::identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> bool all_of(I first, S last, Pred pred, Proj proj = Proj{}) { return ranges::find_if_not(first, last, std::ref(pred), std::ref(proj)) == last; } template<InputRange R, class Proj = ranges::identity, IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred> bool all_of(R&& r, Pred pred, Proj proj = Proj{}) { return ranges::all_of(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } |
| 2番目のバージョン |
template<InputIterator I, Sentinel<I> S, class Proj = ranges::identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> bool any_of(I first, S last, Pred pred, Proj proj = Proj{}) { return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) != last; } template<InputRange R, class Proj = ranges::identity, IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred> bool any_of(R&& r, Pred pred, Proj proj = Proj{}) { return ranges::any_of(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } |
| 第3版 |
template<InputIterator I, Sentinel<I> S, class Proj = identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> bool none_of(I first, S last, Pred pred, Proj proj = Proj{}) { return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) == last; } template<InputRange R, class Proj = ranges::identity, IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred> bool none_of(R&& r, Pred pred, Proj proj = Proj{}) { return ranges::none_of(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } |
[編集] Example
このコードを実行
#include <experimental/ranges/algorithm> #include <experimental/ranges/iterator> #include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> namespace ranges = std::experimental::ranges; 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, ranges::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"; struct DivisibleBy { const int d; DivisibleBy(int n) : d(n) {} bool operator()(int n) const { return n % 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
[編集] See also
| (C++11)(C++11)(C++11) |
範囲内のすべての、いずれかの、またはどの要素も述語が true にならないかをチェックする (関数テンプレート) |