Namespaces
Variants

std:: all_of, std:: any_of, std:: none_of

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
all_of any_of none_of
(C++11) (C++11) (C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
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,

ForwardIt first, ForwardIt last, UnaryPred p ) ;
(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,

ForwardIt first, ForwardIt last, UnaryPred p ) ;
(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,

ForwardIt first, ForwardIt last, UnaryPred p ) ;
(6) (seit C++17)
1) Prüft, ob das unäre Prädikat p für alle Elemente im Bereich [ first , last ) true zurückgibt.
3) Prüft, ob das unäre Prädikat p für mindestens ein Element im Bereich [ first , last ) true zurückgibt.
5) Prüft, ob das unäre Prädikat p für true für keines der Elemente im Bereich [ first , last ) zurückgibt.
2,4,6) Gleich wie (1,3,5) , aber ausgeführt gemäß policy .
Diese Überladungen nehmen nur dann an der Überladungsauflösung teil, wenn alle folgenden Bedingungen erfüllt sind:

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 v vom Typ (möglicherweise const) VT , wobei VT der Werttyp von InputIt ist, unabhängig von der Wertkategorie , in bool konvertierbar sein und darf v nicht modifizieren. Daher ist ein Parametertyp VT & nicht zulässig , ebenso wenig wie VT , es sei denn, für VT ist eine Verschiebung äquivalent zu einer Kopie (seit C++11) . ​

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
  1. Der Bereich ist in diesem Fall leer.

Komplexität

1-6) Höchstens std:: distance ( first, last ) Anwendungen des Prädikats p .

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 ExecutionPolicy einer der Standard-Policies ist, wird std::terminate aufgerufen. Für jede andere ExecutionPolicy ist 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;
}
**Anmerkung:** Da der Text innerhalb der `
`-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

prüft, ob ein Prädikat für true für alle, irgendeine oder keine Elemente in einem Bereich ist
(Algorithmus-Funktionsobjekt)