std::ranges::adjacent_find
Актуально для C++26.
#include <algorithm>
Актуально на 2025-05-22.
Define overload #1
template<forward_iterator I, sentinel_for<I> S, class Proj = identity, indirect_binary_predicate<projected<I, Proj>, projected<I, Proj>> Pred = ranges::equal_to> constexpr I adjacent_find(I first, S last, Pred pred = {}, Proj proj = {});
Ищет в диапазоне [first, last], первую последовательность состоящую из эквивалентных элементов.
Example, possible implementation
Define overload #2
template<forward_range R, class Proj = identity, indirect_binary_predicate<projected<iterator_t<R>, Proj>, projected<iterator_t<R>, Proj>> Pred = ranges::equal_to> constexpr borrowed_iterator_t<R> adjacent_find(R&& r, Pred pred = {}, Proj proj = {});
Ищет в диапазоне "r", первую последовательность состоящую из эквивалентных элементов.
Example, possible implementation
Возвращаемое значение
Вернёт итератор на начало найденного диапазона, если такой найдется, иначе, вернёт итератор на конец диапазона ("last").
Notes
- Во избежание возврата из функции итератора ссылающегося на временный объект, будет возвращен объект std::ranges::dangling, если "r" rvalue.
Сложность
Линейная = min((i - first) + 1, (last - first) - 1).
Examples
Example 1:
#include <iostream> #include <algorithm> #include <string> namespace ranges = std::ranges; using namespace std::string_literals; template<class T> constexpr auto func(T&& t) { auto result = ranges::adjacent_find(std::forward<T>(t)); if constexpr(std::is_same_v<decltype(result), std::ranges::dangling>) { return "t is an rvalue?"; } else return result == t.cend() ? "No matches." : std::string_view{result, t.cend()}; } int main() { constexpr static std::string haystack = "hel11o_world!"; std::cout << func(haystack) << std::endl; std::cout << func("haystack"s) << std::endl; }
11o_world! t is an rvalue?
Changelog
C++20
Введён в стандарт.See also
TODO
This page was last modified on 2025-05-22