std::all_of, std::any_of, std::none_of
Von de.cppreference.net
< cpp | Algorithmen
| Definiert im 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 InputIt, class UnaryPred >
bool any_of( InputIt first, InputIt last, UnaryPred p );
|
(2) | (seit C++11) (constexpr seit C++20) |
template< class InputIt, class UnaryPred >
bool none_of( InputIt first, InputIt last, UnaryPred p );
|
(3) | (seit C++11) (constexpr seit C++20) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred >
bool all_of( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, UnaryPred p );
|
(4) | (seit C++17) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred >
bool any_of( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, UnaryPred p );
|
(5) | (seit C++17) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred >
bool none_of( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, UnaryPred p );
|
(6) | (seit C++17) |
1) Prüft, ob das unäre Prädikat
p true für alle Elemente im Bereich [first, last) zurückgibt.2) Prüft, ob das unäre Prädikat
p true für mindestens ein Element im Bereich [first, last) zurückgibt.3) Prüft, ob das unäre Prädikat
p true für kein Element im Bereich [first, last) zurückgibt.4-6) Wie (1-3), aber ausgeführt gemäß
policy. Diese Überladungen nehmen nur dann an der Überladungsauflösung teil, wenn der Wert des folgenden Ausdrucks
true ist:
|
|
(bis C++20) |
|
|
(seit C++20) |
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 |
| 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 hat ein true Element
|
Ja | Nein | ||
|---|---|---|---|---|
Bereich hat ein false 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.
Komplexität
1-6) Höchstens
std::distance(first, last) Anwendungen von p.Ausnahmen
4-6) Während des Ausführungsprozesses:
- Wenn die für die Parallelisierung erforderlichen temporären Speicherressourcen nicht verfügbar sind, wird std::bad_alloc geworfen.
- Wenn beim Zugriff auf Objekte über ein Algorithmusargument eine nicht abgefangene Ausnahme ausgelöst wird, wird das Verhalten durch die Ausführungsrichtlinie bestimmt (für Standardrichtlinien wird std::terminate aufgerufen).
Anmerkungen
| Feature-Test Makro | Wert | Std | Feature |
|---|---|---|---|
__cpp_lib_parallel_algorithm |
202506L |
(C++26) | Parallele Bereichsalgorithmen, (4-6) |
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;
}
|
Beispiel
Diesen Code ausführen
#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";
using namespace std::placeholders;
if (std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<>(), _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
Siehe auch
(C++20)(C++20)(C++20) |
prüft, ob ein Prädikat true für alle, einige oder keines der Elemente in einem Bereich zutrifft(Algorithmenfunktionsobjekt) |