Namespaces
Variants

std:: find_first_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
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 ForwardIt >

InputIt find_first_of ( InputIt first, InputIt last,

ForwardIt s_first, ForwardIt s_last ) ;
(1) (constexpr seit C++20)
template < class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

ForwardIt1 find_first_of ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last,

ForwardIt2 s_first, ForwardIt2 s_last ) ;
(2) (seit C++17)
template < class InputIt, class ForwardIt, class BinaryPred >

InputIt find_first_of ( InputIt first, InputIt last,
ForwardIt s_first, ForwardIt s_last,

BinaryPred p ) ;
(3) (constexpr seit C++20)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2, class BinaryPred >
ForwardIt1 find_first_of ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt last,
ForwardIt2 s_first, ForwardIt2 s_last,

BinaryPred p ) ;
(4) (seit C++17)

Durchsucht den Bereich [ first , last ) nach einem beliebigen Element aus dem Bereich [ s_first , s_last ) .

1) Elemente werden mit operator == verglichen.
3) Elemente werden mit dem gegebenen binären Prädikat p verglichen.
2,4) Gleich wie (1,3) , 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
s_first, s_last - das Paar von Iteratoren, das den Bereich der zu suchenden Elemente definiert
policy - die zu verwendende Ausführungsrichtlinie
p - binäres Prädikat, das ​ true zurückgibt, wenn die Elemente als gleich behandelt werden sollen.

Die Signatur der Prädikatfunktion sollte äquivalent zu Folgendem sein:

bool pred ( const Type1 & a, const Type2 & b ) ;

Obwohl die Signatur nicht const & benötigt, darf die Funktion die übergebenen Objekte nicht modifizieren und muss alle Werte der Typen (möglicherweise const) Type1 und Type2 unabhängig von der Wertkategorie akzeptieren können (daher ist Type1 & nicht erlaubt , ebenso wenig wie Type1 , es sei denn, für Type1 ist eine Verschiebung äquivalent zu einer Kopie (seit C++11) ).
Die Typen Type1 und Type2 müssen so beschaffen sein, dass Objekte der Typen ForwardIt1 und ForwardIt2 dereferenziert und dann implizit in Type1 bzw. Type2 konvertiert werden können. ​

Typanforderungen
-
InputIt muss die Anforderungen von LegacyInputIterator erfüllen.
-
ForwardIt muss die Anforderungen von LegacyForwardIterator erfüllen.
-
ForwardIt1 muss die Anforderungen von LegacyForwardIterator erfüllen.
-
ForwardIt2 muss die Anforderungen von LegacyForwardIterator erfüllen.
-
BinaryPred muss die Anforderungen von BinaryPredicate erfüllen.

Rückgabewert

Iterator zum ersten Element im Bereich [ first , last ) , das gleich einem Element aus dem Bereich [ s_first , s_last ) ist.

Wenn [ s_first , s_last ) leer ist oder kein solches Element gefunden wird, wird last zurückgegeben.

Komplexität

Gegeben N als std:: distance ( first, last ) und S als std:: distance ( s_first, s_last ) :

1,2) Höchstens N·S Vergleiche unter Verwendung von operator == .
3,4) Höchstens N·S 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

find_first_of (1)
template<class InputIt, class ForwardIt>
InputIt find_first_of(InputIt first, InputIt last,
                      ForwardIt s_first, ForwardIt s_last)
{
    for (; first != last; ++first)
        for (ForwardIt it = s_first; it != s_last; ++it)
            if (*first == *it)
                return first;
    return last;
}
find_first_of (3)
template<class InputIt, class ForwardIt, class BinaryPred>
InputIt find_first_of(InputIt first, InputIt last,
                      ForwardIt s_first, ForwardIt s_last,
                      BinaryPred p)
{
    for (; first != last; ++first)
        for (ForwardIt it = s_first; it != s_last; ++it)
            if (p(*first, *it))
                return first;
    return last;
}
**Hinweis:** Der C++-Code in den `
`-Tags wurde gemäß den Anweisungen nicht übersetzt. Die Funktionsnamen und C++-spezifischen Begriffe (wie `template`, `class`, `InputIt`, etc.) bleiben unverändert.

Beispiel

Der folgende Code sucht nach einem der angegebenen Ganzzahlen in einem Vektor von Ganzzahlen:

#include <algorithm>
#include <iostream>
#include <vector>
auto print_sequence = [](const auto id, const auto& seq, int pos = -1)
{
    std::cout << id << "{ ";
    for (int i{}; auto const& e : seq)
    {
        const bool mark{i == pos};
        std::cout << (i++ ? ", " : "");
        std::cout << (mark ? "[ " : "") << e << (mark ? " ]" : "");
    }
    std::cout << " }\n";
};
int main()
{
    const std::vector<int> v{0, 2, 3, 25, 5};
    const auto t1 = {19, 10, 3, 4};
    const auto t2 = {1, 6, 7, 9};
    auto find_any_of = [](const auto& v, const auto& t)
    {
        const auto result = std::find_first_of(v.begin(), v.end(),
                                               t.begin(), t.end());
        if (result == v.end())
        {
            std::cout << "No elements of v are equal to any element of ";
            print_sequence("t = ", t);
            print_sequence("v = ", v);
        }
        else
        {
            const auto pos = std::distance(v.begin(), result);
            std::cout << "Found a match (" << *result << ") at position " << pos;
            print_sequence(", where t = ", t);
            print_sequence("v = ", v, pos);
        }
    };
    find_any_of(v, t1);
    find_any_of(v, t2);
}

Ausgabe:

Found a match (3) at position 2, where t = { 19, 10, 3, 4 }
v = { 0, 2, [ 3 ], 25, 5 }
No elements of v are equal to any element of t = { 1, 6, 7, 9 }
v = { 0, 2, 3, 25, 5 }

Fehlerberichte

Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.

DR Angewendet auf Verhalten wie veröffentlicht Korrektes Verhalten
LWG 576 C++98 first und last mussten LegacyForwardIterator s sein sie müssen nur
LegacyInputIterator s sein
LWG 1205 C++98 der Rückgabewert war unklar, falls [ s_first , s_last ) leer ist gibt in diesem Fall last zurück

Siehe auch

findet das erste Element, das bestimmte Kriterien erfüllt
(Funktions-Template)
sucht nach einem beliebigen Element aus einer Menge von Elementen
(Algorithmus-Funktionsobjekt)