◐ Shell
clean mode source ↗

std::search_n – cppreference.com

Aus cppreference.com

<metanoindex/>

<tbody> </tbody>

definiert in Header

<algorithm>

template< class ForwardIt, class Size, class T > ForwardIt1 search_n( ForwardIt first, ForwardIt last, Size count, const T& value );

(1)

template< class ForwardIt, class Size, class T, class BinaryPredicate > ForwardIt1 search_n( ForwardIt first, ForwardIt last, Size count, const T& value, BinaryPredicate p );

(2)

Searches der Bereich [first, last) für die erste Folge des Grafen identische Elemente, die jeweils gleich dem angegebenen Wert Wert. Die erste Version verwendet operator== um die Elemente zu vergleichen, verwendet die zweite Version des gegebenen binären Prädikats p .

Original:

Searches the range [first, last) for the first sequence of count identical elements, each equal to the given value value. The first version uses operator== to compare the elements, the second version uses the given binary predicate p.

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

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.

count -

die Länge der Sequenz zu suchen

Original:

the length of the sequence to search for

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

value -

der Wert der Elemente, nach dem gesucht

Original:

the value of the elements to search for

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

p - binary predicate which returns ​true if the elements should be treated as equal.

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

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

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

Type requirements
-ForwardIt must meet the requirements of ForwardIterator.

Rückgabewert

Iiterator an den Anfang des gefundenen Sequenz im Bereich [first, last). Wenn keine solche Sequenz gefunden wird, wird zurückgegeben last .

Original:

Iiterator to the beginning of the found sequence in the range [first, last). If no such sequence is found, last is returned.

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

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 ForwardIt, class Size, class T>
ForwardIt1 search_n(ForwardIt first, ForwardIt last,
                          Size count, const T& value)
{
    Size curr_count = 0;
    ForwardIt result, t_last = first;
    std::advance(t_last, std::distance(first, last) - count + 1);
 
    for (; first != t_last; first++) {
        curr_count = 0;
        result = first;
        while (*first == value) {
            curr_count++;
            if (curr_count == count) {
                return result;
            }
            ++first;
        }
    }
    return last;
}
Second version
template<class ForwardIt, class Size, class T, class BinaryPredicate>
ForwardIt1 search_n(ForwardIt first, ForwardIt last,
                          Size count, const T& value, BinaryPredicate p)
{
    Size curr_count = 0;
    ForwardIt result, t_last = first;
    std::advance(t_last, std::distance(first, last) - count + 1);
 
    for (; first != t_last; first++) {
        curr_count = 0;
        result = first;
        while (p(*first == value)) {
            curr_count++;
            if (curr_count == count) {
                return result;
            }
            ++first;
        }
    }
    return last;
}

Beispiel

Siehe auch

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]
Findet das erste Elemente, welches bestimmte Kriterien erfüllt
(Funktions-Template) [edit]
searches for a range of elements
(Funktions-Template) [edit]