std::search
| Заголовочный файл <algorithm>
|
||
| template< class ForwardIt1, class ForwardIt2 > ForwardIt1 search( ForwardIt1 first, ForwardIt1 last, |
(1) | |
| template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > ForwardIt1 search( ForwardIt1 first, ForwardIt1 last, |
(2) | |
Ищет первое вхождение последовательности элементов [s_first, s_last) в диапазон [first, last - (s_last - s_first)). Первый вариант использует operator== для сравнения элементов, второй вариант использует заданный бинарный предикат p.
Содержание |
[править] Параметры
| first, last | - | диапазон элементов для проверки | |||||||||
| s_first, s_last | - | диапазон искомых элементов | |||||||||
| 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:
The signature does not need to have const &, but the function must not modify the objects passed to it. | |||||||||
| Type requirements | |||||||||||
-ForwardIt1, ForwardIt2 must meet the requirements of ForwardIterator.
| |||||||||||
[править] Возвращаемое значение
Итератор на начало первой подпоследовательности [s_first, s_last) в диапазоне [first, last - (s_last - s_first)). Если такая последовательность не найдена, возвращается last.
Если [s_first, s_last) пуста, возвращается first. (начиная с C++11)
[править] Сложность
Не больше S*N сравнений, где S = std::distance(s_first, s_last), N = std::distance(first, last).
[править] Возможная реализация
| First version |
|---|
template<class ForwardIt1, class ForwardIt2> ForwardIt1 search(ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last) { for (; ; ++first) { ForwardIt1 it = first; for (ForwardIt2 s_it = s_first; ; ++it, ++s_it) { if (s_it == s_last) { return first; } if (it == last) { return last; } if (!(*it == *s_it)) { break; } } } } |
| Second version |
template<class ForwardIt1, class ForwardIt2, class BinaryPredicate> ForwardIt1 search(ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p) { for (; ; ++first) { ForwardIt1 it = first; for (ForwardIt2 s_it = s_first; ; ++it, ++s_it) { if (s_it == s_last) { return first; } if (it == last) { return last; } if (!p(*it, *s_it)) { break; } } } } |
[править] Пример
| Этот раздел не завершён |
[править] См. также
| находит последний последовательности элементов в определенном диапазоне 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. (шаблон функции) | |
| определяет, будет ли два множества элементов одинаковы Original: determines if two sets of elements are the same The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (шаблон функции) | |
| (C++11) |
находит первый элемент, удовлетворяющий определенным критериям Original: finds the first element satisfying specific criteria The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (шаблон функции) |
| возвращает истину, если один диапазон лексикографически меньше, чем другой Original: returns true if one range is lexicographically less than another The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (шаблон функции) | |
| находит первое положение, в котором два диапазона отличаются Original: finds the first position where two ranges differ The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (шаблон функции) | |
| поиск в течение ряда последовательных копий элемента в диапазоне Original: searches for a number consecutive copies of an element in a range The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (шаблон функции) | |