◐ Shell
clean mode source ↗

std::find, std::find_if, std::find_if_not – cppreference.com

Aus cppreference.com

<tbody> </tbody>

definiert in Header

<algorithm>

template< class InputIt, class T > InputIt find( InputIt first, InputIt last, const T& value );

(1)

template< class InputIt, class UnaryPredicate > InputIt find_if( InputIt first, InputIt last, UnaryPredicate p );

(2)

template< class InputIt, class UnaryPredicate > InputIt find_if_not( InputIt first, InputIt last, UnaryPredicate q );

(3) (seit C++11)

Diese Funktionen finden das erste Element im Bereich [first, last), dass bestimmte Kriterien erfüllt:

1. find Suche nach einem Element gleich value

2. find_if sucht nach einem Element, für das Prädikat p true wird

3. find_if_not sucht Element, für das Prädikat q false wird

Parameter

first, last -

das Spektrum der Elemente zu untersuchen

Original:

the range of elements to examine

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

value -

Wert auf die Elemente zu vergleichen

Original:

value to compare the elements to

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

p - unary predicate which returns ​true

für das gewünschte Element

Original:

for the required element

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

.

The signature of the predicate function should be equivalent to the following:

bool pred(const Type &a);

The signature does not need to have const &, but the function must not modify the objects passed to it.
The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. ​

q - unary predicate which returns ​false

für das gewünschte Element

Original:

for the required element

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

.

The signature of the predicate function should be equivalent to the following:

bool pred(const Type &a);

The signature does not need to have const &, but the function must not modify the objects passed to it.
The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. ​

Type requirements
-InputIt must meet the requirements of InputIterator.

Rückgabewert

Iterator auf das erste Element, das die Bedingung erfüllt oder last wenn kein solches Element gefunden wird.

Komplexität

An den meisten last - first Anwendungen des Prädikats

Original:

At most last - first applications of the predicate

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Mögliche Implementierung

First version
template<class InputIt, class T>
InputIt find(InputIt first, InputIt last, const T& value)
{
    for (; first != last; ++first) {
        if (*first == value) {
            return first;
        }
    }
    return last;
}
Second version
template<class InputIt, class UnaryPredicate>
InputIt find_if(InputIt first, InputIt last, UnaryPredicate p)
{
    for (; first != last; ++first) {
        if (p(*first)) {
            return first;
        }
    }
    return last;
}
Third version
template<class InputIt, class UnaryPredicate>
InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q)
{
    for (; first != last; ++first) {
        if (!q(*first)) {
            return first;
        }
    }
    return last;
}

Wenn Sie keinen C + +11, ein Äquivalent zu std::find_if_not ist std::find_if mit dem negierten Prädikat verwenden .

Original:

If you do not have C++11, an equivalent to std::find_if_not is to use std::find_if with the negated predicate.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

template<class InputIt, class UnaryPredicate>
InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q)
{
    return std::find_if(first, last, std::not1(q));
}

Beispiel

Im folgenden Beispiel wird eine ganze Zahl in einem Vektor von ganzen Zahlen .

Original:

The following example finds an integer in a vector of integers.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <iostream>
#include <algorithm>
#include <vector>

int main()
{
    int n1 = 3;
    int n2 = 5;
 
    std::vector<int> v{0, 1, 2, 3, 4};
 
    auto result1 = std::find(v.begin(), v.end(), n1);
    auto result2 = std::find(v.begin(), v.end(), n2);
 
    if (result1 != v.end()) {
        std::cout << "v contains: " << n1 << '\n';
    } else {
        std::cout << "v does not contain: " << n1 << '\n';
    }

    if (result2 != v.end()) {
        std::cout << "v contains: " << n2 << '\n';
    } else {
        std::cout << "v does not contain: " << n2 << '\n';
    }
}

Output:

v contains: 3
v does not contain: 5

Siehe auch

findet zwei identischen (oder eine andere Beziehung) Elemente einander benachbart

Original:

finds two identical (or some other relationship) items adjacent to each other

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(Funktions-Template) [edit]

findet die letzte Sequenz von Elementen in einem bestimmten Bereich

Original:

finds the last sequence of elements in a certain range

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(Funktions-Template) [edit]
searches for any one of a set of elements
(Funktions-Template) [edit]
Findet die erste Position, an der sich zwei Bereiche unterscheiden
(Funktions-Template) [edit]
searches for a range of elements
(Funktions-Template) [edit]