Search⌘ K

Using Searchers

Understand how C++17's searchers work to enable efficient and reusable string matching. This lesson covers the use of searcher objects for pattern preprocessing and repeated searches across multiple text ranges, setting the stage for in-depth practical examples.

We'll cover the following...

The std::search function uses the following overload for searchers:

C++
template<class ForwardIterator, class Searcher>
ForwardIterator search(ForwardIterator first, ForwardIterator last, const Searcher& searcher );

For example:

C++ 17
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string testString = "Hello Super World";
string needle = "Super";
const auto it = search(begin(testString), end(testString), boyer_moore_searcher(begin(needle), end(needle)));
if (it == cend(testString))
cout << "The string " << needle << " not found\n";
return 0;
}

Each searcher initializes its state ...