Files
hustoj/web/cppreference-zh/chmhelp/cpp-utility-functional-boyer_moore_horspool_searcher.html
2025-08-01 12:57:54 +08:00

180 lines
16 KiB
HTML

<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs" xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh">
<head><meta http-equiv="x-ua-compatible" content="ie=edge">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>std::boyer_moore_horspool_searcher</title>
<link rel="stylesheet" href="ext.css" />
<link rel="stylesheet" href="site_modules.css" />
</head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_utility_functional_boyer_moore_horspool_searcher skin-cppreference2 action-view cpp-navbar">
<div id="cpp-content-base">
<div id="content"><a id="top"></a>
<h1 id="firstHeading" class="firstHeading"><span style="font-size:0.7em; line-height:130%">std::</span>boyer_moore_horspool_searcher</h1>
<div id="bodyContent">
<div id="contentSub"><span class="subpages">&lt; <a href="cpp.html">cpp</a>&lrm; | <a href="cpp-utility.html">utility</a>&lrm; | <a href="cpp-utility-functional.html">functional</a></span></div>
<div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr" xml:lang="zh">
<table class="t-dcl-begin">
<tbody>
<tr class="t-dsc-header">
<td>
<div>&#23450;&#20041;&#20110;&#22836;&#25991;&#20214; <code><a href="cpp-header-functional.html">&lt;functional&gt;</a></code></div>
</td>
<td></td>
<td></td>
</tr>
<tr class="t-dcl t-since-cxx17">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span> <span class="kw1">class</span> RandomIt1,<br /></span>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">class</span> Hash <span class="sy1">=</span> <a href="cpp-utility-hash.html"><span class="kw1122">std::<span class="me2">hash</span></span></a><span class="sy1">&lt;</span><span class="kw1">typename</span> <a href="cpp-iterator-iterator_traits.html"><span class="kw651">std::<span class="me2">iterator_traits</span></span></a><span class="sy1">&lt;</span>RandomIt1<span class="sy1">&gt;</span><span class="sy4">::</span><span class="me2">value_type</span><span class="sy1">&gt;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">class</span> BinaryPredicate <span class="sy1">=</span> <a href="cpp-utility-functional-equal_to.html"><span class="kw1035">std::<span class="me2">equal_to</span></span></a><span class="sy1">&lt;&gt;</span> <span class="sy1">&gt;</span><br /></p>
<span class="kw1">class</span> boyer_moore_horspool_searcher<span class="sy4">;</span></div>
</td>
<td class="t-dcl-nopad"></td>
<td><span class="t-mark-rev t-since-cxx17">(C++17 &#36215;)</span></td>
</tr>
<tr class="t-dcl-sep">
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p>&#36866;&#29992;&#20110; <span class="t-lc"><a href="cpp-algorithm-search.html">std::search</a></span> &#30340;<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/named_req/Searcher&amp;action=edit&amp;redlink=1" class="new" title="cpp/named req/Searcher&#65288;&#39029;&#38754;&#19981;&#23384;&#22312;&#65289;"><span style="font-style:italic">&#25628;&#32034;&#22120;</span> <span style="font-family: Georgia, &#39;DejaVu Serif&#39;, serif; font-style:italic">(Searcher)</span></a> &#30340;&#37325;&#36733;&#65292;&#23454;&#29616;&#20102; <a rel="nofollow" class="external text" href="https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore%E2%80%93Horspool_algorithm">Boyer-Moore-Horspool &#23383;&#31526;&#20018;&#25628;&#32034;&#31639;&#27861;</a>&#12290;</p>
<p><code>boyer_moore_horspool_searcher</code> &#20026;<a href="cpp-named_req-CopyConstructible.html"><span style="font-style:italic">&#21487;&#22797;&#21046;&#26500;&#36896;</span> <span style="font-family: Georgia, &#39;DejaVu Serif&#39;, serif; font-style:italic">(CopyConstructible)</span></a> &#21450;<a href="cpp-named_req-CopyAssignable.html"><span style="font-style:italic">&#21487;&#22797;&#21046;&#36171;&#20540;</span> <span style="font-family: Georgia, &#39;DejaVu Serif&#39;, serif; font-style:italic">(CopyAssignable)</span></a> &#12290;</p>
<p><code>RandomIt1</code> &#24517;&#39035;&#28385;&#36275;<a href="cpp-named_req-RandomAccessIterator.html"><span style="font-style:italic">&#32769;&#24335;&#38543;&#26426;&#35775;&#38382;&#36845;&#20195;&#22120;</span> <span style="font-family: Georgia, &#39;DejaVu Serif&#39;, serif; font-style:italic">(LegacyRandomAccessIterator)</span></a> &#30340;&#35201;&#27714;&#12290;</p>
<h3><span class="mw-headline" id=".E6.88.90.E5.91.98.E5.87.BD.E6.95.B0">&#25104;&#21592;&#20989;&#25968;</span></h3>
<div class="t-member">
<h2><span class="mw-headline" id="std::boyer_moore_horspool_searcher::boyer_moore_horspool_searcher"><span style="font-size:0.7em; line-height:130%">std::boyer_moore_horspool_searcher::</span>boyer_moore_horspool_searcher</span></h2>
<table class="t-dcl-begin">
<tbody>
<tr class="t-dcl">
<td class="t-dcl-nopad">
<div><span class="mw-geshi cpp source-cpp">boyer_moore_horspool_searcher<span class="br0">(</span> RandomIt1 pat_first,<br /></span>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RandomIt1 pat_last,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Hash hf <span class="sy1">=</span> Hash<span class="br0">(</span><span class="br0">)</span>,<br /></p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BinaryPredicate pred <span class="sy1">=</span> BinaryPredicate<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span></div>
</td>
<td class="t-dcl-nopad"></td>
<td class="t-dcl-nopad"></td>
</tr>
<tr class="t-dcl-sep">
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p>&#36890;&#36807;&#23384;&#20648; <code>pat_first</code> &#12289; <code>pat_last</code> &#12289; <code>hf</code> &#21644; <code>pred</code> &#30340;&#21103;&#26412;&#26500;&#36896;&#19968;&#20010; <code>boyer_moore_horspool_searcher</code> &#65292;&#23433;&#35013;&#20219;&#20309;&#38656;&#35201;&#30340;&#20869;&#37096;&#25968;&#25454;&#32467;&#26500;&#12290;</p>
<p><code>RandomIt1</code> &#30340;&#20540;&#24517;&#39035;&#26159;<a href="cpp-named_req-DefaultConstructible.html"><span style="font-style:italic">&#21487;&#40664;&#35748;&#26500;&#36896;</span> <span style="font-family: Georgia, &#39;DejaVu Serif&#39;, serif; font-style:italic">(DefaultConstructible)</span></a> &#12289;<a href="cpp-named_req-CopyConstructible.html"><span style="font-style:italic">&#21487;&#22797;&#21046;&#26500;&#36896;</span> <span style="font-family: Georgia, &#39;DejaVu Serif&#39;, serif; font-style:italic">(CopyConstructible)</span></a> &#21450;<a href="cpp-named_req-CopyAssignable.html"><span style="font-style:italic">&#21487;&#22797;&#21046;&#36171;&#20540;</span> <span style="font-family: Georgia, &#39;DejaVu Serif&#39;, serif; font-style:italic">(CopyAssignable)</span></a> &#12290;</p>
<p>&#23545;&#20110;&#20219;&#24847;&#20108;&#20010; <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="cpp-iterator-iterator_traits.html"><span class="kw651">std::<span class="me2">iterator_traits</span></span></a><span class="sy1">&lt;</span>RandomIt1<span class="sy1">&gt;</span><span class="sy4">::</span><span class="me2">value_type</span></span></span> &#31867;&#22411;&#30340;&#20540; <code>A</code> &#21644; <code>B</code> &#65292;&#33509; <span class="t-c"><span class="mw-geshi cpp source-cpp">pred<span class="br0">(</span>A, B<span class="br0">)</span> <span class="sy1">==</span> <span class="kw2">true</span></span></span> &#65292;&#21017; <span class="t-c"><span class="mw-geshi cpp source-cpp">hf<span class="br0">(</span>A<span class="br0">)</span> <span class="sy1">==</span> hf<span class="br0">(</span>B<span class="br0">)</span></span></span> &#24212;&#20026; <code>true</code> &#12290;</p>
<h3><span class="mw-headline" id=".E5.8F.82.E6.95.B0">&#21442;&#25968;</span></h3>
<table class="t-par-begin">
<tr class="t-par">
<td>pat_first, pat_last</td>
<td>-</td>
<td>&#34920;&#31034;&#35201;&#25628;&#32034;&#30340;&#23383;&#31526;&#20018;&#30340;&#19968;&#23545;&#36845;&#20195;&#22120;</td>
</tr>
<tr class="t-par">
<td>hf</td>
<td>-</td>
<td>&#29992;&#20110;&#21704;&#24076;&#23383;&#31526;&#20018;&#20803;&#32032;&#30340;&#21487;&#35843;&#29992;&#23545;&#35937;</td>
</tr>
<tr class="t-par">
<td>pred</td>
<td>-</td>
<td>&#30830;&#23450;&#30456;&#31561;&#30340;&#21487;&#35843;&#29992;&#23545;&#35937;</td>
</tr>
</table>
<h3><span class="mw-headline" id=".E5.BC.82.E5.B8.B8">&#24322;&#24120;</span></h3>
<p>&#20219;&#20309;&#20250;&#20026;&#19979;&#21015;&#32773;&#25243;&#20986;&#30340;&#24322;&#24120;</p>
<ul>
<li><code>RandomIt1</code> &#30340;&#22797;&#21046;&#26500;&#36896;&#20989;&#25968;&#65307;</li>
<li><code>RandomIt1</code> &#30340; value_type &#30340;&#40664;&#35748;&#26500;&#36896;&#20989;&#25968;&#12289;&#22797;&#21046;&#26500;&#36896;&#20989;&#25968;&#25110;&#22797;&#21046;&#36171;&#20540;&#36816;&#31639;&#31526;&#65307;&#25110;</li>
<li><code>BinaryPredicate</code> &#25110; <code>Hash</code> &#30340;&#22797;&#21046;&#26500;&#36896;&#20989;&#25968;&#25110;&#20989;&#25968;&#35843;&#29992;&#36816;&#31639;&#31526;.</li>
</ul>
<p>&#33509;&#20026;&#20869;&#37096;&#25968;&#25454;&#32467;&#26500;&#35831;&#27714;&#30340;&#38468;&#21152;&#20869;&#23384;&#26080;&#27861;&#20998;&#37197;&#65292;&#21017;&#20134;&#21487;&#33021;&#25243;&#20986; <span class="t-lc"><a href="cpp-memory-new-bad_alloc.html">std::bad_alloc</a></span> &#12290;</p>
</div>
<div class="t-member">
<h2><span class="mw-headline" id="std::boyer_moore_horspool_searcher::operator.28.29"><span style="font-size:0.7em; line-height:130%">std::boyer_moore_horspool_searcher::</span>operator()</span></h2>
<table class="t-dcl-begin">
<tbody>
<tr class="t-dcl">
<td class="t-dcl-nopad">
<div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span> <span class="kw1">class</span> RandomIt2 <span class="sy1">&gt;</span><br />
<a href="cpp-utility-pair.html"><span class="kw1094">std::<span class="me2">pair</span></span></a><span class="sy1">&lt;</span>RandomIt2,RandomIt2<span class="sy1">&gt;</span> operator<span class="br0">(</span><span class="br0">)</span><span class="br0">(</span> RandomIt2 first, RandomIt2 last <span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></span></div>
</td>
<td class="t-dcl-nopad"></td>
<td class="t-dcl-nopad"></td>
</tr>
<tr class="t-dcl-sep">
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p><span class="t-lc"><a href="cpp-algorithm-search.html">std::search</a></span> &#30340;&#25628;&#32034;&#22120;&#65288;Searcher&#65289;&#37325;&#36733;&#35843;&#29992;&#30340;&#35813;&#25104;&#21592;&#20989;&#25968;&#65292;&#20197;&#36827;&#34892;&#29992;&#27492;&#25628;&#32034;&#22120;&#30340;&#25628;&#32034;&#12290; <code>RandomIt2</code> &#24517;&#39035;&#28385;&#36275;<a href="cpp-named_req-RandomAccessIterator.html"><span style="font-style:italic">&#32769;&#24335;&#38543;&#26426;&#35775;&#38382;&#36845;&#20195;&#22120;</span> <span style="font-family: Georgia, &#39;DejaVu Serif&#39;, serif; font-style:italic">(LegacyRandomAccessIterator)</span></a> &#30340;&#35201;&#27714;&#12290;</p>
<p><code>RandomIt1</code> &#19982; <code>RandomIt2</code> &#24517;&#39035;&#25317;&#26377;&#30456;&#21516;&#30340;&#20540;&#31867;&#22411;&#12290;</p>
<h3><span class="mw-headline" id=".E5.8F.82.E6.95.B0_2">&#21442;&#25968;</span></h3>
<table class="t-par-begin">
<tr class="t-par">
<td>first, last</td>
<td>-</td>
<td>&#25351;&#20195;&#35201;&#34987;&#26816;&#39564;&#30340;&#23383;&#31526;&#20018;&#30340;&#19968;&#23545;&#36845;&#20195;&#22120;</td>
</tr>
</table>
<h3><span class="mw-headline" id=".E8.BF.94.E5.9B.9E.E5.80.BC">&#36820;&#22238;&#20540;</span></h3>
<p>&#33509;&#27169;&#24335;&#65288; <span class="texhtml" style="white-space: nowrap;">[pat_first, pat_last)</span> &#65289;&#20026;&#31354;&#65292;&#21017;&#36820;&#22238; <code>make_pair(first, first)</code> &#12290;</p>
<p>&#21542;&#21017;&#65292;&#36820;&#22238;&#20110; <span class="texhtml" style="white-space: nowrap;">[first, last)</span> &#20013;&#25351;&#21521;&#39318;&#21450;&#23614;&#21518;&#19968;&#20301;&#30340;&#19968;&#23545;&#36845;&#20195;&#22120;&#65292;&#20854;&#20013;&#23450;&#20301;&#20102;&#19982; <span class="texhtml" style="white-space: nowrap;">[pat_first, pat_last)</span> &#30001; <code>pred</code> &#25152;&#23450;&#20041;&#27604;&#36739;&#30456;&#31561;&#30340;&#23376;&#24207;&#21015;&#65292;&#21542;&#21017;&#36820;&#22238; <code>make_pair(last, last)</code> &#12290;</p>
</div>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">&#31034;&#20363;</span></h3>
<div class="t-example">
<div class="t-example-live-link"></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;">
<div class="cpp source-cpp">
<pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="co2">#include &lt;string&gt;</span>
<span class="co2">#include &lt;algorithm&gt;</span>
<span class="co2">#include &lt;functional&gt;</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
<a href="cpp-string-basic_string.html"><span class="kw1228">std::<span class="me2">string</span></span></a> in <span class="sy1">=</span> <span class="st0">&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit,&quot;</span>
<span class="st0">&quot; sed do eiusmod tempor incididunt ut labore et dolore magna aliqua&quot;</span><span class="sy4">;</span>
<a href="cpp-string-basic_string.html"><span class="kw1228">std::<span class="me2">string</span></span></a> needle <span class="sy1">=</span> <span class="st0">&quot;pisci&quot;</span><span class="sy4">;</span>
<span class="kw4">auto</span> it <span class="sy1">=</span> <a href="cpp-algorithm-search.html"><span class="kw1578">std::<span class="me2">search</span></span></a><span class="br0">(</span>in.<span class="me1">begin</span><span class="br0">(</span><span class="br0">)</span>, in.<span class="me1">end</span><span class="br0">(</span><span class="br0">)</span>,
std<span class="sy4">::</span><span class="me2">boyer_moore_horspool_searcher</span><span class="br0">(</span>
needle.<span class="me1">begin</span><span class="br0">(</span><span class="br0">)</span>, needle.<span class="me1">end</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw1">if</span><span class="br0">(</span>it <span class="sy3">!</span><span class="sy1">=</span> in.<span class="me1">end</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span>
<a href="cpp-io-cout.html"><span class="kw1756">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot;The string &quot;</span> <span class="sy1">&lt;&lt;</span> needle <span class="sy1">&lt;&lt;</span> <span class="st0">&quot; found at offset &quot;</span>
<span class="sy1">&lt;&lt;</span> it <span class="sy2">-</span> in.<span class="me1">begin</span><span class="br0">(</span><span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">&#39;<span class="es1">\n</span>&#39;</span><span class="sy4">;</span>
<span class="kw1">else</span>
<a href="cpp-io-cout.html"><span class="kw1756">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot;The string &quot;</span> <span class="sy1">&lt;&lt;</span> needle <span class="sy1">&lt;&lt;</span> <span class="st0">&quot; not found<span class="es1">\n</span>&quot;</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div>
</div>
<p>&#36755;&#20986;&#65306;</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;">
<div class="text source-text">
<pre class="de1">The string pisci found at offset 43</pre></div>
</div>
</div>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">&#21442;&#38405;</span></h3>
<table class="t-dsc-begin">
<tr class="t-dsc">
<td>
<div class="t-dsc-member-div">
<div><a href="cpp-algorithm-search.html"><span class="t-lines"><span>search</span></span></a></div>
</div>
</td>
<td>&#25628;&#32034;&#19968;&#20010;&#20803;&#32032;&#33539;&#22260;<br />
<span class="t-mark">(&#20989;&#25968;&#27169;&#26495;)</span></td>
</tr>
</table>
</div>
<div class="visualClear"></div>
</div>
</div>
</div>
</body>
</html>