A searcher suitable for use with the Searcher overload of std::search that implements the Boyer-Moore string searching algorithm.
std::boyer_moore_searcher
is CopyConstructible and CopyAssignable.
RandomIt1
must meet the requirements of LegacyRandomAccessIterator.
RandomIt1 pat_last,
Hash hf = Hash(),
Constructs a std::boyer_moore_searcher
by storing copies of pat_first, pat_last, hf, and pred, setting up any necessary internal data structures.
The value type of RandomIt1
must be DefaultConstructible, CopyConstructible and CopyAssignable.
For any two values A
and B
of the type std::iterator_traits<RandomIt1>::value_type, if pred(A, B) == true, then hf(A) == hf(B) shall be true.
Any exceptions thrown by
RandomIt1
;RandomIt1
; orBinaryPredicate
or Hash
.May also throw std::bad_alloc if additional memory required for internal data structures cannot be allocated.
std::boyer_moore_searcher::operator() template< class RandomIt2 >The member function called by the Searcher overload of std::search to perform a search with this searcher. RandomIt2
must meet the requirements of LegacyRandomAccessIterator.
RandomIt1
and RandomIt2
must have the same value type.
If the pattern [
pat_first,
pat_last)
is empty, returns std::make_pair(first, first).
Otherwise, returns a pair of iterators to the first and one past last positions in [
first,
last)
where a subsequence that compares equal to [
pat_first,
pat_last)
as defined by pred is located, or std::make_pair(last, last) otherwise.
#include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <string_view> int main() { constexpr std::string_view haystack = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed " "do eiusmod tempor incididunt ut labore et dolore magna aliqua"; const std::string_view needle{"pisci"}; if (const auto it = std::search(haystack.begin(), haystack.end(), std::boyer_moore_searcher(needle.begin(), needle.end())); it != haystack.end() ) std::cout << "The string " << std::quoted(needle) << " found at offset " << it - haystack.begin() << '\n'; else std::cout << "The string " << std::quoted(needle) << " not found\n"; }
Output:
The string "pisci" found at offset 43[edit] See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4