Ежедневный бит(e) C++ #253. Алгоритм поиска подпоследовательности: std::search.

Алгоритм std::search возвращает первый экземпляр подпоследовательности.

Вариант C++17 поддерживает как параллельное выполнение, так и пользовательские поисковые системы. Пользовательские поисковые системы предлагают более высокую среднюю сложность (вплоть до линейной).

#include <string>
#include <algorithm>
#include <functional>

std::string text = "the quick brown fox jumps over the lazy dog";
std::string needle = "fox";

// Find the first instance of a sub-sequence in text
auto it = std::search(text.begin(), text.end(), 
                      needle.begin(), needle.end());
// it != text.end()

std::string_view word(it, it+needle.length());
// word == "fox"

// C++17 introduced searchers that offer better average complexity
std::boyer_moore_horspool_searcher searcher(
  needle.begin(), needle.end());
// Average linear complexity
it = std::search(text.begin(), text.end(), searcher);
// Same behaviour as default search
// it != text.end()
word = std::string_view(it, it+needle.length());
// word == "fox"

// Range version doesn't support searchers and returns a subrange
auto [begin, end] = std::ranges::search(text, needle);
word = std::string_view(begin,end);
// word == "fox"

Откройте пример в Compiler Explorer.