std::ranges:: all_of, std::ranges:: any_of, std::ranges:: none_of
|
Definiert in Header
<algorithm>
|
||
|
Aufrufsignatur
|
||
|
template
<
std::
input_iterator
I,
std::
sentinel_for
<
I
>
S,
class
Proj
=
std::
identity
,
|
(1) | (seit C++20) |
|
template
<
ranges::
input_range
R,
class
Proj
=
std::
identity
,
std::
indirect_unary_predicate
<
|
(2) | (seit C++20) |
|
template
<
std::
input_iterator
I,
std::
sentinel_for
<
I
>
S,
class
Proj
=
std::
identity
,
|
(3) | (seit C++20) |
|
template
<
ranges::
input_range
R,
class
Proj
=
std::
identity
,
std::
indirect_unary_predicate
<
|
(4) | (seit C++20) |
|
template
<
std::
input_iterator
I,
std::
sentinel_for
<
I
>
S,
class
Proj
=
std::
identity
,
|
(5) | (seit C++20) |
|
template
<
ranges::
input_range
R,
class
Proj
=
std::
identity
,
std::
indirect_unary_predicate
<
|
(6) | (seit C++20) |
[
first
,
last
)
false
zurückgibt (nach der Projektion mit der Projektion
proj
).
[
first
,
last
)
true
zurückgibt (nach der Projektion mit der Projektion
proj
).
[
first
,
last
)
zurückgibt (nach Projektion mit der Projektion
proj
).
Die auf dieser Seite beschriebenen funktionsähnlichen Entitäten sind Algorithm Function Objects (informell bekannt als Niebloids ), das heißt:
- Explizite Template-Argumentlisten können beim Aufruf keiner von ihnen angegeben werden.
- Keiner von ihnen ist sichtbar für argument-dependent lookup .
- Wenn einer von ihnen durch normal unqualified lookup als Name links vom Funktionsaufrufoperator gefunden wird, wird argument-dependent lookup unterdrückt.
Inhaltsverzeichnis |
Parameter
| first, last | - | das Iterator-Sentinel-Paar, das den Bereich der zu untersuchenden Elemente definiert |
| r | - | der Bereich der zu untersuchenden Elemente |
| pred | - | Prädikat, das auf die projizierten Elemente angewendet wird |
| proj | - | Projektion, die auf die Elemente angewendet wird |
Rückgabewert
| Bereich enthält einige true Elemente | Ja | Nein | ||
|---|---|---|---|---|
| Bereich enthält einige false Elemente | Ja | Nein | Ja | Nein [1] |
all_of
|
false | true | false | true |
any_of
|
true | true | false | false |
none_of
|
false | false | true | true |
- ↑ Der Bereich ist in diesem Fall leer.
Komplexität
Höchstens last - first Anwendungen des Prädikats und der Projektion.
Mögliche Implementierung
| 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; |
Beispiel
#include <algorithm> #include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> namespace ranges = std::ranges; constexpr bool some_of(auto&& r, auto&& pred) // einige aber nicht alle { 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 << "Unter den Zahlen: "; 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 << "Alle Zahlen sind gerade\n"; if (ranges::none_of(v, std::bind(std::modulus<int>(), std::placeholders::_1, 2))) std::cout << "Keine davon ist ungerade\n"; auto DivisibleBy = [](int d) { return [d](int m) { return m % d == 0; }; }; if (ranges::any_of(v, DivisibleBy(7))) std::cout << "Mindestens eine Zahl ist durch 7 teilbar\n"; }
Ausgabe:
Unter den Zahlen: 2 4 6 8 10 12 14 16 18 20 Alle Zahlen sind gerade Keine davon ist ungerade Mindestens eine Zahl ist durch 7 teilbar
Siehe auch
|
(C++11)
(C++11)
(C++11)
|
prüft, ob ein Prädikat für
true
für alle, irgendeine oder keine Elemente in einem Bereich steht
(Funktions-Template) |