std:: all_of, std:: any_of, std:: none_of
|
Definiert in Header
<algorithm>
|
||
|
template
<
class
InputIt,
class
UnaryPred
>
bool all_of ( InputIt first, InputIt last, UnaryPred p ) ; |
(1) |
(seit C++11)
(constexpr seit C++20) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
UnaryPred
>
bool
all_of
(
ExecutionPolicy
&&
policy,
|
(2) | (seit C++17) |
|
template
<
class
InputIt,
class
UnaryPred
>
bool any_of ( InputIt first, InputIt last, UnaryPred p ) ; |
(3) |
(seit C++11)
(constexpr seit C++20) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
UnaryPred
>
bool
any_of
(
ExecutionPolicy
&&
policy,
|
(4) | (seit C++17) |
|
template
<
class
InputIt,
class
UnaryPred
>
bool none_of ( InputIt first, InputIt last, UnaryPred p ) ; |
(5) |
(seit C++11)
(constexpr seit C++20) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
UnaryPred
>
bool
none_of
(
ExecutionPolicy
&&
policy,
|
(6) | (seit C++17) |
[
first
,
last
)
true
zurückgibt.
[
first
,
last
)
true
zurückgibt.
[
first
,
last
)
zurückgibt.
|
std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> ist true . |
(bis C++20) |
|
std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> ist true . |
(seit C++20) |
Inhaltsverzeichnis |
Parameter
| first, last | - | das Paar von Iteratoren, das den Bereich der zu untersuchenden Elemente definiert |
| policy | - | die zu verwendende Ausführungsrichtlinie |
| p | - |
unäres Prädikat.
Der Ausdruck
p
(
v
)
muss für jedes Argument
|
| Typanforderungen | ||
-
InputIt
muss die Anforderungen von
LegacyInputIterator
erfüllen.
|
||
-
ForwardIt
muss die Anforderungen von
LegacyForwardIterator
erfüllen.
|
||
-
UnaryPred
muss die Anforderungen von
Predicate
erfüllen.
|
||
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
Ausnahmen
Die Überladungen mit einem Template-Parameter namens
ExecutionPolicy
melden Fehler wie folgt:
-
Wenn die Ausführung einer als Teil des Algorithmus aufgerufenen Funktion eine Exception wirft und
ExecutionPolicyeiner der Standard-Policies ist, wird std::terminate aufgerufen. Für jede andereExecutionPolicyist das Verhalten implementierungsdefiniert. - Wenn der Algorithmus keinen Speicher allozieren kann, wird std::bad_alloc geworfen.
Mögliche Implementierung
Siehe auch die Implementierungen von
| all_of |
|---|
template<class InputIt, class UnaryPred> constexpr bool all_of(InputIt first, InputIt last, UnaryPred p) { return std::find_if_not(first, last, p) == last; } |
| any_of |
template<class InputIt, class UnaryPred> constexpr bool any_of(InputIt first, InputIt last, UnaryPred p) { return std::find_if(first, last, p) != last; } |
| none_of |
template<class InputIt, class UnaryPred> constexpr bool none_of(InputIt first, InputIt last, UnaryPred p) { return std::find_if(first, last, p) == last; } |
`-Tags C++-Code enthält und gemäß den Anweisungen nicht übersetzt werden soll, sowie die Funktionsnamen `all_of`, `any_of`, `none_of` als C++-spezifische Begriffe erhalten bleiben müssen, wurde nur die strukturelle Übersetzung durchgeführt. Der eigentliche Code und die Funktionsnamen bleiben unverändert.
Beispiel
#include <algorithm> #include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> int main() { std::vector<int> v(10, 2); std::partial_sum(v.cbegin(), v.cend(), v.begin()); std::cout << "Unter den Zahlen: "; std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; if (std::all_of(v.cbegin(), v.cend(), [](int i) { return i % 2 == 0; })) std::cout << "Alle Zahlen sind gerade\n"; if (std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<>(), std::placeholders::_1, 2))) std::cout << "Keine davon ist ungerade\n"; struct DivisibleBy { const int d; DivisibleBy(int n) : d(n) {} bool operator()(int n) const { return n % d == 0; } }; if (std::any_of(v.cbegin(), v.cend(), 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++20)
(C++20)
(C++20)
|
prüft, ob ein Prädikat für
true
für alle, irgendeine oder keine Elemente in einem Bereich ist
(Algorithmus-Funktionsobjekt) |