std::boyer_moore_searcher(3)

๐Ÿ“ฆ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

๐Ÿ’– NAME ๐Ÿ’–

Okay so welcome to the std::boyer_moore_searcher page from the C++ Standard Libary manual, bestie ๐Ÿ’… The name is std::boyer_moore_searcher - std::boyer_moore_searcher, and yes that's the whole NAME section, no cap โœจ

๐ŸŒˆ Synopsis ๐ŸŒˆ

Defined in header <functional>.

The template signature is literally this, so copy it exactly:

template< class RandomIt1,

          class Hash = std::hash<typename
          std::iterator_traits<RandomIt1>::value_type>,
       (since C++17)
                    class BinaryPredicate = std::equal_to<> >

          class boyer_moore_searcher;

So this class is a searcher suitable for use with the Searcher overload of std::search, and it implements the Boyer-Moore string searching algorithm. Iconic. ๐ŸŽ€

std::boyer_moore_searcher is CopyConstructible and CopyAssignable.

RandomIt1 must meet the requirements of LegacyRandomAccessIterator, fr fr ๐Ÿฆ‹

๐Ÿงš Member functions ๐Ÿงš

๐Ÿ’– std::boyer_moore_searcher::boyer_moore_searcher ๐Ÿ’–

boyer_moore_searcher( RandomIt1 pat_first,

                      RandomIt1 pat_last,
                      Hash hf = Hash(),

                      BinaryPredicate pred = BinaryPredicate() );

This constructor constructs a std::boyer_moore_searcher by storing copies of pat_first, pat_last, hf, and pred, and 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. That's the vibe check ๐ŸŒธ

๐Ÿ’• Parameters ๐Ÿ’•

  • pat_first, pat_last โ€” a pair of iterators designating the string to be searched for ๐Ÿ”
  • hf โ€” a callable object used to hash the elements of the string ๐Ÿงฎ
  • pred โ€” a callable object used to determine equality ๐Ÿซถ

๐Ÿ’€ Exceptions ๐Ÿ’€

Any exceptions thrown by:

  • the copy constructor of RandomIt1;
  • the default constructor, copy constructor, and copy assignment operator of the value type of RandomIt1; or
  • the copy constructor and function call operator of BinaryPredicate or Hash.

May also throw std::bad_alloc if additional memory required for internal data structures cannot be allocated. So yeah, memory said no, careful ๐Ÿ˜ญ

๐ŸŒŸ std::boyer_moore_searcher::operator() ๐ŸŒŸ

          template< class RandomIt2 >
          std::pair<RandomIt2, RandomIt2> operator()( RandomIt2 first,
       (since C++17)
          RandomIt2 last ) const;

This is 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. ๐Ÿ”ฅ

๐Ÿ’• Parameters ๐Ÿ’•

  • first, last โ€” a pair of iterators designating the string to be examined ๐Ÿ”Ž

๐ŸŽ€ Return value ๐ŸŽ€

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. That's the whole return-value tea. ๐Ÿต

๐Ÿ“ Notes ๐Ÿ“

Feature-test macro vibe check:

  • Feature-test macro: __cpp_lib_boyer_moore_searcher
  • Value: 201603L
  • Std: (C++17)
  • Feature: searchers

That's the whole table, bestie โœจ

๐Ÿ“ Example ๐Ÿ“

// Run this code

        #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

๐Ÿ‘€ See also ๐Ÿ‘€

  • search โ€” searches for a range of elements (function template) ๐Ÿ”
  • default_searcher โ€” standard C++ library search algorithm implementation (C++17) (class template) ๐Ÿงš
  • boyer_moore_horspool_searcher โ€” Boyer-Moore-Horspool search algorithm implementation (C++17) (class template) ๐Ÿฆ‹

xoxo, your Unix bestie ๐Ÿ’– โ€” source/version: http://cppreference.com; date: 2024.06.10; page: std::boyer_moore_searcher(3). Bye bestie ๐Ÿซถโœจ

๐Ÿ“ฆ where this page comes from