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

189 lines
14 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::default_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_default_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>default_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> ForwardIt, <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 />
<span class="kw1">class</span> default_searcher<span class="sy4">;</span></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;&#21512;&#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> &#37325;&#36733;&#30340;&#31867;&#65292;&#23427;&#23558;&#25628;&#32034;&#25805;&#20316;&#22996;&#27966;&#21040; C++17 &#21069;&#26631;&#20934;&#24211;&#30340; <span class="t-lc"><a href="cpp-algorithm-search.html">std::search</a></span> &#12290;</p>
<p><code>default_searcher</code> &#26159;&#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>
<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::default_searcher::default_searcher"><span style="font-size:0.7em; line-height:130%">std::default_searcher::</span>default_searcher</span></h2>
<table class="t-dcl-begin">
<tbody></tbody>
<tbody class="t-dcl-rev">
<tr class="t-dcl t-since-cxx17 t-until-cxx20">
<td>
<div><span class="mw-geshi cpp source-cpp">default_searcher<span class="br0">(</span> ForwardIt pat_first,<br /></span>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ForwardIt pat_last,<br /></p>
&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><span class="t-mark-rev t-since-cxx17">(C++17 &#36215;)</span><br />
<span class="t-mark-rev t-until-cxx20">(C++20 &#21069;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx20">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw4">constexpr</span> default_searcher<span class="br0">(</span> ForwardIt pat_first,<br /></span>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ForwardIt pat_last,<br /></p>
&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><span class="t-mark-rev t-since-cxx20">(C++20 &#36215;)</span></td>
</tr>
</tbody>
<tbody>
<tr class="t-dcl-sep">
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p>&#20197;&#23384;&#20648; <code>pat_first</code> &#12289; <code>pat_last</code> &#21450; <code>pred</code> &#30340;&#21103;&#26412;&#26500;&#36896;&#19968;&#20010; <code>default_searcher</code></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>&#25351;&#20195;&#35201;&#25628;&#32034;&#30340;&#20018;&#30340;&#19968;&#23545;&#36845;&#20195;&#22120;</td>
</tr>
<tr class="t-par">
<td>pred</td>
<td>-</td>
<td>&#29992;&#20110;&#26816;&#27979;&#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; <code>BinaryPredicate</code> &#25110; <code>ForwardIt</code> &#30340;&#22797;&#21046;&#26500;&#36896;&#20989;&#25968;&#21487;&#33021;&#25243;&#20986;&#30340;&#24322;&#24120;&#12290;</p>
</div>
<div class="t-member">
<h2><span class="mw-headline" id="std::default_searcher::operator.28.29"><span style="font-size:0.7em; line-height:130%">std::default_searcher::</span>operator()</span></h2>
<table class="t-dcl-begin">
<tbody></tbody>
<tbody class="t-dcl-rev">
<tr class="t-dcl t-since-cxx17 t-until-cxx20">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span> <span class="kw1">class</span> ForwardIt2 <span class="sy1">&gt;</span><br /></span>
<p><a href="cpp-utility-pair.html"><span class="kw1094">std::<span class="me2">pair</span></span></a><span class="sy1">&lt;</span>ForwardIt2, ForwardIt2<span class="sy1">&gt;</span><br /></p>
&nbsp; &nbsp; operator<span class="br0">(</span><span class="br0">)</span><span class="br0">(</span> ForwardIt2 first, ForwardIt2 last <span class="br0">)</span> <span class="kw4">const</span><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><br />
<span class="t-mark-rev t-until-cxx20">(C++20 &#21069;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx20">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span> <span class="kw1">class</span> ForwardIt2 <span class="sy1">&gt;</span><br /></span>
<p><span class="kw4">constexpr</span> <a href="cpp-utility-pair.html"><span class="kw1094">std::<span class="me2">pair</span></span></a><span class="sy1">&lt;</span>ForwardIt2, ForwardIt2<span class="sy1">&gt;</span><br /></p>
&nbsp; &nbsp; operator<span class="br0">(</span><span class="br0">)</span><span class="br0">(</span> ForwardIt2 first, ForwardIt2 last <span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></div>
</td>
<td class="t-dcl-nopad"></td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 &#36215;)</span></td>
</tr>
</tbody>
<tbody>
<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; (<span style="font-style:italic">&#25628;&#32034;&#22120;</span> <span style="font-family: Georgia, &#39;DejaVu Serif&#39;, serif; font-style:italic">(Searcher)</span> ) &#37325;&#36733;&#35843;&#29992;&#27492;&#20989;&#25968;&#20197;&#36827;&#34892;&#29992;&#27492;&#25628;&#32034;&#22120;&#30340;&#25628;&#32034;&#12290;</p>
<p>&#36820;&#22238;&#19968;&#23545;&#36845;&#20195;&#22120; <code>i, j</code> &#65292;&#20854;&#20013; <code>i</code> &#20026; <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="cpp-algorithm-search.html"><span class="kw1578">std::<span class="me2">search</span></span></a><span class="br0">(</span>first, last, pat_first, pat_last, pred<span class="br0">)</span></span></span> &#19988; <code>j</code> &#20026; <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="cpp-iterator-next.html"><span class="kw674">std::<span class="me2">next</span></span></a><span class="br0">(</span>i, <a href="cpp-iterator-distance.html"><span class="kw673">std::<span class="me2">distance</span></span></a><span class="br0">(</span>pat_first, pat_last<span class="br0">)</span><span class="br0">)</span></span></span> &#65292;&#38500;&#38750; <code>std::search</code> &#36820;&#22238; <code>last</code> &#65288;&#26080;&#21305;&#37197;&#65289;&#65292;&#27492;&#24773;&#20917;&#19979; <code>j</code> &#20063;&#31561;&#20110; <code>last</code> &#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;&#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>&#20998;&#21035;&#25351;&#21521; <span class="texhtml" style="white-space: nowrap;">[first, last)</span> &#20013;&#39318;&#20301;&#32622;&#21644;&#23614;&#21518;&#19968;&#20301;&#30340;&#19968;&#23545;&#36845;&#20195;&#22120;&#65292;&#20854;&#20013;&#23450;&#20301;&#30340;&#23376;&#24207;&#21015;&#25353; <code>pred</code> &#25152;&#23450;&#20041;&#19982; <span class="texhtml" style="white-space: nowrap;">[pat_first, pat_last)</span> &#27604;&#36739;&#30456;&#31561;&#65292;&#20854;&#20182;&#24773;&#20917;&#36820;&#22238;&#19968;&#23545; <code>last</code> &#30340;&#21103;&#26412;&#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">default_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>