Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/functional/boyer moore horspool searcher"

From cppreference.com
< cpp‎ | utility‎ | functional
(title; example)
m ({{c}}, <string_view>, +std::quoted)
 
(11 intermediate revisions by 6 users not shown)
Line 3: Line 3:
  
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | functional }}
+
{{dcl header|functional}}
{{dcl | since=c++17 |
+
{{dcl|since=c++17|1=
 
template< class RandomIt1,
 
template< class RandomIt1,
           class Hash {{=}} std::hash<typename std::iterator_traits<RandomIt1>::value_type>,
+
           class Hash = std::hash<typename std::iterator_traits<RandomIt1>::value_type>,
           class BinaryPredicate {{=}} std::equal_to<> >
+
           class BinaryPredicate = std::equal_to<> >
 
class boyer_moore_horspool_searcher;
 
class boyer_moore_horspool_searcher;
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
A searcher suitable for use with the {{concept|Searcher}} overload of {{lc|std::search}} that implements the [https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore%E2%80%93Horspool_algorithm Boyer-Moore-Horspool string searching algorithm].
+
A searcher suitable for use with the {{named req|Searcher}} overload of {{lc|std::search}} that implements the {{enwiki|Boyer%E2%80%93Moore%E2%80%93Horspool algorithm|Boyer-Moore-Horspool string searching algorithm}}.
  
{{tt|boyer_moore_horspool_searcher}} is {{concept|CopyConstructible}} and {{concept|CopyAssignable}}.
+
{{tt|std::boyer_moore_horspool_searcher}} is {{named req|CopyConstructible}} and {{named req|CopyAssignable}}.
  
{{tt|RandomIt1}} must meet the requirements of {{concept|RandomAccessIterator}}.
+
{{tt|RandomIt1}} must meet the requirements of {{named req|RandomAccessIterator}}.
  
 
===Member functions===
 
===Member functions===
{{member | {{small|std::boyer_moore_horspool_searcher::}}boyer_moore_horspool_searcher | 2=
+
{{member|{{small|std::boyer_moore_horspool_searcher::}}boyer_moore_horspool_searcher|2=
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl |  
+
{{dcl|1=
 
boyer_moore_horspool_searcher( RandomIt1 pat_first,
 
boyer_moore_horspool_searcher( RandomIt1 pat_first,
 
                               RandomIt1 pat_last,
 
                               RandomIt1 pat_last,
                               Hash hf {{=}} Hash(),
+
                               Hash hf = Hash(),
                               BinaryPredicate pred {{=}} BinaryPredicate());
+
                               BinaryPredicate pred = BinaryPredicate() );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Constructs a {{tt|boyer_moore_horspool_searcher}} by storing copies of {{tt|pat_first}}, {{tt|pat_last}}, {{tt|hf}}, and {{tt|pred}}, setting up any necessary internal data structures.  
+
Constructs a {{tt|std::boyer_moore_horspool_searcher}} by storing copies of {{c|pat_first}}, {{c|pat_last}}, {{c|hf}}, and {{c|pred}}, setting up any necessary internal data structures.  
  
The value type of {{tt|RandomIt1}} must be {{concept|DefaultConstructible}}, {{concept|CopyConstructible}} and {{concept|CopyAssignable}}.
+
The value type of {{tt|RandomIt1}} must be {{named req|DefaultConstructible}}, {{named req|CopyConstructible}} and {{named req|CopyAssignable}}.
  
For any two values {{tt|A}} and {{tt|B}} of the type {{c|std::iterator_traits<RandomIt1>::value_type}}, if {{c|pred(A, B) {{==}} true}}, then {{c|hf(A) {{==}} hf(B)}} shall be {{tt|true}}.
+
For any two values {{tt|A}} and {{tt|B}} of the type {{c|std::iterator_traits<RandomIt1>::value_type}}, if {{c|1= pred(A, B) == true}}, then {{c|1= hf(A) == hf(B)}} shall be {{c|true}}.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | pat_first, pat_last | a pair of iterators designating the string to be searched for}}
+
{{par|pat_first, pat_last|a pair of iterators designating the string to be searched for}}
{{par | hf | a callable object used to hash the elements of the string }}
+
{{par|hf|a callable object used to hash the elements of the string}}
{{par | pred | a callable object used to determine equality }}
+
{{par|pred|a callable object used to determine equality}}
 
{{par end}}
 
{{par end}}
  
Line 49: Line 49:
 
}}
 
}}
  
{{member | {{small|std::boyer_moore_horspool_searcher::}}operator() | 2=
+
{{member|{{small|std::boyer_moore_horspool_searcher::}}operator()|2=
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl |
+
{{dcl|
 
template< class RandomIt2 >
 
template< class RandomIt2 >
std::pair<RandomIt2,RandomIt2> operator()( RandomIt2 first, RandomIt2 last ) const;
+
std::pair<RandomIt2, RandomIt2> operator()( RandomIt2 first, RandomIt2 last ) const;
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
The member function called by the Searcher overload of {{lc|std::search}} to perform a search with this searcher. {{tt|RandomIt2}} must meet the requirements of {{concept|RandomAccessIterator}}.
+
The member function called by the Searcher overload of {{lc|std::search}} to perform a search with this searcher. {{tt|RandomIt2}} must meet the requirements of {{named req|RandomAccessIterator}}.
  
 
{{tt|RandomIt1}} and {{tt|RandomIt2}} must have the same value type.
 
{{tt|RandomIt1}} and {{tt|RandomIt2}} must have the same value type.
Line 62: Line 62:
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | first, last | a pair of iterators designating the string to be examined}}
+
{{par|first, last|a pair of iterators designating the string to be examined}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
If the pattern ({{math|[pat_first, pat_last)}}) is empty, returns {{tt|make_pair(first, first)}}.
+
If the pattern {{range|pat_first|pat_last}} is empty, returns {{c|std::make_pair(first, first)}}.
  
Otherwise, returns a pair of iterators to the first and one past last positions in {{math|[first, last)}} where a subsequence that compares equal to {{math|[pat_first, pat_last)}} as defined by {{tt|pred}} is located, or {{tt|make_pair(last, last)}} otherwise.
+
Otherwise, returns a pair of iterators to the first and one past last positions in {{range|first|last}} where a subsequence that compares equal to {{range|pat_first|pat_last}} as defined by {{c|pred}} is located, or {{c|std::make_pair(last, last)}} otherwise.
 
}}
 
}}
 +
 +
===Notes===
 +
{{feature test macro|__cpp_lib_boyer_moore_searcher|[[cpp/utility/functional#Searchers|searchers]]|value=201603L|std=C++17}}
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
|code=
+
#include <iostream>
+
#include <string>
+
 
#include <algorithm>
 
#include <algorithm>
 
#include <functional>
 
#include <functional>
 +
#include <iomanip>
 +
#include <iostream>
 +
#include <string_view>
 
   
 
   
 
int main()
 
int main()
 
{
 
{
     std::string in = "Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
+
     constexpr std::string_view in =
                    " sed do eiusmod tempor incididunt ut labore et dolore magna aliqua";
+
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed "
     std::string needle = "pisci";
+
        "do eiusmod tempor incididunt ut labore et dolore magna aliqua";
 +
 
 +
     const std::string_view needle{"pisci"};
 +
 
 
     auto it = std::search(in.begin(), in.end(),
 
     auto it = std::search(in.begin(), in.end(),
                  std::boyer_moore_horspool_searcher(
+
                  std::boyer_moore_horspool_searcher(
                      needle.begin(), needle.end())).first;
+
                      needle.begin(), needle.end()));
     if(it != in.end())
+
     if (it != in.end())
         std::cout << "The string " << needle << " found at offset "
+
         std::cout << "The string " << std::quoted(needle) << " found at offset "
 
                   << it - in.begin() << '\n';
 
                   << it - in.begin() << '\n';
 
     else
 
     else
         std::cout << "The string " << needle << " not found\n";
+
         std::cout << "The string " << std::quoted(needle) << " not found\n";
 
}
 
}
 
|output=
 
|output=
The string pisci found at offset 43
+
The string "pisci" found at offset 43
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/algorithm/dsc search}}
+
{{dsc inc|cpp/algorithm/dsc search}}
 +
{{dsc inc|cpp/utility/functional/dsc default_searcher}}
 +
{{dsc inc|cpp/utility/functional/dsc boyer_moore_searcher}}
 
{{dsc end}}
 
{{dsc end}}
 +
 +
{{langlinks|de|es|ja|ru|zh}}

Latest revision as of 11:27, 24 May 2023

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Function objects
Function invocation
(C++17)(C++23)
Identity function object
(C++20)
Transparent operator wrappers
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

Negators
(C++17)
Searchers
boyer_moore_horspool_searcher
(C++17)    
Old binders and adaptors
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)  
(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
(until C++17*)(until C++17*)
(until C++17*)(until C++17*)

(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
 
Defined in header <functional>
template< class RandomIt1,

          class Hash = std::hash<typename std::iterator_traits<RandomIt1>::value_type>,
          class BinaryPredicate = std::equal_to<> >

class boyer_moore_horspool_searcher;
(since C++17)

A searcher suitable for use with the Searcher overload of std::search that implements the Boyer-Moore-Horspool string searching algorithm.

std::boyer_moore_horspool_searcher is CopyConstructible and CopyAssignable.

RandomIt1 must meet the requirements of LegacyRandomAccessIterator.

Contents

[edit] Member functions

std::boyer_moore_horspool_searcher::boyer_moore_horspool_searcher

boyer_moore_horspool_searcher( RandomIt1 pat_first,

                               RandomIt1 pat_last,
                               Hash hf = Hash(),

                               BinaryPredicate pred = BinaryPredicate() );

Constructs a std::boyer_moore_horspool_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.

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, or copy assignment operator of the value type of RandomIt1; or
  • the copy constructor or function call operator of BinaryPredicate or Hash.

May also throw std::bad_alloc if additional memory required for internal data structures cannot be allocated.

std::boyer_moore_horspool_searcher::operator()

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

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_firstpat_last) is empty, returns std::make_pair(first, first).

Otherwise, returns a pair of iterators to the first and one past last positions in [firstlast) where a subsequence that compares equal to [pat_firstpat_last) as defined by pred is located, or std::make_pair(last, last) otherwise.

[edit] Notes

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

[edit] Example

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string_view>
 
int main()
{
    constexpr std::string_view in =
        "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"};
 
    auto it = std::search(in.begin(), in.end(),
                  std::boyer_moore_horspool_searcher(
                      needle.begin(), needle.end()));
    if (it != in.end())
        std::cout << "The string " << std::quoted(needle) << " found at offset "
                  << it - in.begin() << '\n';
    else
        std::cout << "The string " << std::quoted(needle) << " not found\n";
}

Output:

The string "pisci" found at offset 43

[edit] See also

searches for the first occurrence of a range of elements
(function template) [edit]
standard C++ library search algorithm implementation
(class template) [edit]
Boyer-Moore search algorithm implementation
(class template) [edit]