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) false zurückgibt.[first, last) true zurückgibt.[first, last) true 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) |
Inhalt |
[edit] Parameter
| first, last | - | das Iteratorenpaar, das den Bereich der zu untersuchenden Elemente definiert |
| policy | - | die Ausführungsrichtlinie, die verwendet werden soll |
| 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. | ||
[edit] Rückgabewert
| Hat wahres Element | Ja | Nein | ||
|---|---|---|---|---|
| Hat falsches Element | 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.
[edit] Komplexität
[edit] Ausnahmen
Die Überladungen mit einem Template-Parameter namens ExecutionPolicy berichten Fehler wie folgt
- Wenn die Ausführung einer Funktion, die als Teil des Algorithmus aufgerufen wird, eine Ausnahme auslöst und
ExecutionPolicyeine der Standardrichtlinien ist, wird std::terminate aufgerufen. Für jede andereExecutionPolicyist das Verhalten implementierungsabhängig. - Wenn dem Algorithmus der Speicher zur Neuzuweisung fehlt, wird std::bad_alloc ausgelöst.
[edit] 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; } |
[edit] 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 << "Among the numbers: "; 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 << "All numbers are even\n"; if (std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<>(), 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 (std::any_of(v.cbegin(), v.cend(), DivisibleBy(7))) std::cout << "At least one number is divisible by 7\n"; }
Ausgabe
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] Siehe auch
| (C++20)(C++20)(C++20) |
Prüft, ob eine Bedingung für alle, einige oder keine Elemente in einem Bereich wahr ist (Algorithmus-Funktionsobjekt) |