1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>binary_search</title> 5<link rel="stylesheet" href="../../../../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../../../index.html" title="Chapter 1. Range 2.0"> 8<link rel="up" href="../non_mutating.html" title="Non-mutating algorithms"> 9<link rel="prev" href="adjacent_find.html" title="adjacent_find"> 10<link rel="next" href="count.html" title="count"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="adjacent_find.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../non_mutating.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="count.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h5 class="title"> 27<a name="range.reference.algorithms.non_mutating.binary_search"></a><a class="link" href="binary_search.html" title="binary_search">binary_search</a> 28</h5></div></div></div> 29<h6> 30<a name="range.reference.algorithms.non_mutating.binary_search.h0"></a> 31 <span class="phrase"><a name="range.reference.algorithms.non_mutating.binary_search.prototype"></a></span><a class="link" href="binary_search.html#range.reference.algorithms.non_mutating.binary_search.prototype">Prototype</a> 32 </h6> 33<p> 34</p> 35<pre class="programlisting"><span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">ForwardRange</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Value</span><span class="special">></span> 36<span class="keyword">bool</span> <span class="identifier">binary_search</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">ForwardRange</span><span class="special">&</span> <span class="identifier">rng</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">Value</span><span class="special">&</span> <span class="identifier">val</span><span class="special">);</span> 37 38<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">ForwardRange</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Value</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">BinaryPredicate</span><span class="special">></span> 39<span class="keyword">bool</span> <span class="identifier">binary_search</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">ForwardRange</span><span class="special">&</span> <span class="identifier">rng</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">Value</span><span class="special">&</span> <span class="identifier">val</span><span class="special">,</span> <span class="identifier">BinaryPredicate</span> <span class="identifier">pred</span><span class="special">);</span> 40</pre> 41<p> 42 </p> 43<h6> 44<a name="range.reference.algorithms.non_mutating.binary_search.h1"></a> 45 <span class="phrase"><a name="range.reference.algorithms.non_mutating.binary_search.description"></a></span><a class="link" href="binary_search.html#range.reference.algorithms.non_mutating.binary_search.description">Description</a> 46 </h6> 47<p> 48 <code class="computeroutput"><span class="identifier">binary_search</span></code> returns 49 <code class="computeroutput"><span class="keyword">true</span></code> if and only if the 50 value <code class="computeroutput"><span class="identifier">val</span></code> exists in the 51 range <code class="computeroutput"><span class="identifier">rng</span></code>. 52 </p> 53<h6> 54<a name="range.reference.algorithms.non_mutating.binary_search.h2"></a> 55 <span class="phrase"><a name="range.reference.algorithms.non_mutating.binary_search.definition"></a></span><a class="link" href="binary_search.html#range.reference.algorithms.non_mutating.binary_search.definition">Definition</a> 56 </h6> 57<p> 58 Defined in the header file <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">range</span><span class="special">/</span><span class="identifier">algorithm</span><span class="special">/</span><span class="identifier">binary_search</span><span class="special">.</span><span class="identifier">hpp</span></code> 59 </p> 60<h6> 61<a name="range.reference.algorithms.non_mutating.binary_search.h3"></a> 62 <span class="phrase"><a name="range.reference.algorithms.non_mutating.binary_search.requirements"></a></span><a class="link" href="binary_search.html#range.reference.algorithms.non_mutating.binary_search.requirements">Requirements</a> 63 </h6> 64<p> 65 <span class="bold"><strong>For the non-predicate versions of binary_search:</strong></span> 66 </p> 67<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 68<li class="listitem"> 69 <code class="computeroutput"><span class="identifier">ForwardRange</span></code> is a 70 model of the <a class="link" href="../../../concepts/forward_range.html" title="Forward Range">Forward 71 Range</a> Concept. 72 </li> 73<li class="listitem"> 74 <code class="computeroutput"><span class="identifier">Value</span></code> is a model 75 of the <code class="computeroutput"><span class="identifier">LessThanComparableConcept</span></code>. 76 </li> 77<li class="listitem"> 78 The ordering of objects of type <code class="computeroutput"><span class="identifier">Value</span></code> 79 is a <span class="bold"><strong><span class="emphasis"><em>strict weak ordering</em></span></strong></span>, 80 as defined in the <code class="computeroutput"><span class="identifier">LessThanComparableConcept</span></code> 81 requirements. 82 </li> 83<li class="listitem"> 84 <code class="computeroutput"><span class="identifier">ForwardRange</span></code>'s value 85 type is the same type as <code class="computeroutput"><span class="identifier">Value</span></code>. 86 </li> 87</ul></div> 88<p> 89 <span class="bold"><strong>For the predicate versions of binary_search:</strong></span> 90 </p> 91<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 92<li class="listitem"> 93 <code class="computeroutput"><span class="identifier">ForwardRange</span></code> is a 94 model of the <a class="link" href="../../../concepts/forward_range.html" title="Forward Range">Forward 95 Range</a> Concept. 96 </li> 97<li class="listitem"> 98 <code class="computeroutput"><span class="identifier">BinaryPredicate</span></code> is 99 a model of the <code class="computeroutput"><span class="identifier">StrictWeakOrderingConcept</span></code>. 100 </li> 101<li class="listitem"> 102 <code class="computeroutput"><span class="identifier">ForwardRange</span></code>'s value 103 type is the same type as <code class="computeroutput"><span class="identifier">Value</span></code>. 104 </li> 105<li class="listitem"> 106 <code class="computeroutput"><span class="identifier">ForwardRange</span></code>'s value 107 type is convertible to <code class="computeroutput"><span class="identifier">BinaryPredicate</span></code>'s 108 argument type. 109 </li> 110</ul></div> 111<h6> 112<a name="range.reference.algorithms.non_mutating.binary_search.h4"></a> 113 <span class="phrase"><a name="range.reference.algorithms.non_mutating.binary_search.precondition_"></a></span><a class="link" href="binary_search.html#range.reference.algorithms.non_mutating.binary_search.precondition_">Precondition:</a> 114 </h6> 115<p> 116 <span class="bold"><strong>For the non-predicate version:</strong></span> 117 </p> 118<p> 119 <code class="computeroutput"><span class="identifier">rng</span></code> is ordered in ascending 120 order according to <code class="computeroutput"><span class="keyword">operator</span><span class="special"><</span></code>. 121 </p> 122<p> 123 <span class="bold"><strong>For the predicate version:</strong></span> 124 </p> 125<p> 126 <code class="computeroutput"><span class="identifier">rng</span></code> is ordered in ascending 127 order according to the function object <code class="computeroutput"><span class="identifier">pred</span></code>. 128 </p> 129<h6> 130<a name="range.reference.algorithms.non_mutating.binary_search.h5"></a> 131 <span class="phrase"><a name="range.reference.algorithms.non_mutating.binary_search.complexity"></a></span><a class="link" href="binary_search.html#range.reference.algorithms.non_mutating.binary_search.complexity">Complexity</a> 132 </h6> 133<p> 134 For non-random-access ranges, the complexity is <code class="computeroutput"><span class="identifier">O</span><span class="special">(</span><span class="identifier">N</span><span class="special">)</span></code> where <code class="computeroutput"><span class="identifier">N</span></code> 135 is <code class="computeroutput"><span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng</span><span class="special">)</span></code>. 136 </p> 137<p> 138 For random-access ranges, the complexity is <code class="computeroutput"><span class="identifier">O</span><span class="special">(</span><span class="identifier">log</span> <span class="identifier">N</span><span class="special">)</span></code> 139 where <code class="computeroutput"><span class="identifier">N</span></code> is <code class="computeroutput"><span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng</span><span class="special">)</span></code>. 140 </p> 141</div> 142<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 143<td align="left"></td> 144<td align="right"><div class="copyright-footer">Copyright © 2003-2010 Thorsten Ottosen, 145 Neil Groves<p> 146 Distributed under the Boost Software License, Version 1.0. (See accompanying 147 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 148 </p> 149</div></td> 150</tr></table> 151<hr> 152<div class="spirit-nav"> 153<a accesskey="p" href="adjacent_find.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../non_mutating.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="count.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> 154</div> 155</body> 156</html> 157