@specsoftdev live:.cid.8e17e9b93cabb607 specsoftdev@gmail.com
std::ranges::find_end
Актуально для C++26.

#include <algorithm>
Актуально на 2025-05-19.


Define overload #1
template<forward_iterator I1, sentinel_for<I1> S1,
         forward_iterator I2, sentinel_for<I2> S2,
         class Pred = ranges::equal_to,
         class Proj1 = identity,
         class Proj2 = identity>
requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
constexpr subrange<I1>
    find_end(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},
             Proj1 proj1 = {}, Proj2 proj2 = {});

Ищет в диапазоне [first, last], крайнее вхождение [s_first, s_last].
Example, possible implementation
Define overload #2
template<forward_range R1, forward_range R2,
         class Pred = ranges::equal_to,
         class Proj1 = identity,
         class Proj2 = identity>
requires indirectly_comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
constexpr borrowed_subrange_t<R1>
    find_end(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {});

Ищет в диапазоне r1, крайнее вхождение r2.
Example, possible implementation

Возвращаемое значение

Вернёт ranges::subrange содержащий пару итераторов указывающих на начало и конец диапазона.



Notes

  • Поиск выполняется последовательно, начиная с конца диапазона, если тип диапазона соответствует концепции std::bidirectional_iterator. Иначе, с начала.




Сложность

Линейная -  (last2 - first2) * (last1 - first1 - (last2 - first2) + 1).



Examples


Example 1:
#include <iostream>
#include <algorithm>
#include <string>

namespace ranges = std::ranges;


int main()
{
    std::string haystack = "hel1o_world!";
    std::string needle = "wo";

    auto result = ranges::find_end(haystack, needle);
    if (!result.empty())
    {
        std::cout << std::string_view(result.begin(), result.end()) << std::endl;
    }
    else
    {
        std::cout << "No matches." << std::endl;
    }
    return 0;
}


wo






Changelog

C++20
Введён в стандарт.


See also

TODO

This page was last modified on 2025-05-19