<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>libstdc++: Binary Search</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="navtree.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="navtree.js"></script> <script type="text/javascript" src="resize.js"></script> <script type="text/javascript"> $(document).ready(initResizable); </script> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.7.4 --> <div id="top"> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> <td style="padding-left: 0.5em;"> <div id="projectname">libstdc++</div> </td> </tr> </tbody> </table> </div> </div> <div id="side-nav" class="ui-resizable side-nav-resizable"> <div id="nav-tree"> <div id="nav-tree-contents"> </div> </div> <div id="splitbar" style="-moz-user-select:none;" class="ui-resizable-handle"> </div> </div> <script type="text/javascript"> initNavTree('a01186.html',''); </script> <div id="doc-content"> <div class="header"> <div class="summary"> <a href="#func-members">Functions</a> </div> <div class="headertitle"> <div class="title">Binary Search</div> </div> <div class="ingroups"><a class="el" href="a01184.html">Sorting</a></div></div> <div class="contents"> <div class="dynheader"> Collaboration diagram for Binary Search:</div> <div class="dyncontent"> <center><table><tr><td><img src="a01186.png" border="0" alt="" usemap="#a01186"/> <map name="a01186" id="a01186"> <area shape="rect" id="node2" href="a01184.html" title="Sorting" alt="" coords="5,5,67,35"/></map> </td></tr></table></center> </div> <h2><a name="func-members"></a> Functions</h2> <ul> <li>template<typename _ForwardIterator , typename _Tp > bool <a class="el" href="a01186.html#ga5957126a4e963070896e9c4ae5221820">std::binary_search</a> (_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val) <li>template<typename _ForwardIterator , typename _Tp , typename _Compare > bool <a class="el" href="a01186.html#ga9fc545c99c9b622c68d3b61ba04f326e">std::binary_search</a> (_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val, _Compare __comp) <li>template<typename _ForwardIterator , typename _Tp , typename _Compare > pair< _ForwardIterator, <br class="typebreak"/> _ForwardIterator > <a class="el" href="a01186.html#ga7941ea333830e800e324ae3e022e1f46">std::equal_range</a> (_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val, _Compare __comp) <li>template<typename _ForwardIterator , typename _Tp > pair< _ForwardIterator, <br class="typebreak"/> _ForwardIterator > <a class="el" href="a01186.html#gab12325ed36d6e07b06b3cbe74bec2845">std::equal_range</a> (_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val) <li>template<typename _ForwardIterator , typename _Tp , typename _Compare > _ForwardIterator <a class="el" href="a01186.html#ga0ff3b53e875d75731ff8361958fac68f">std::lower_bound</a> (_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val, _Compare __comp) <li>template<typename _ForwardIterator , typename _Tp > _ForwardIterator <a class="el" href="a01186.html#gabe324553abc3238696e8e2660bfa5c66">std::lower_bound</a> (_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val) <li>template<typename _ForwardIterator , typename _Tp , typename _Compare > _ForwardIterator <a class="el" href="a01186.html#gaac066ef92d4b5059d7609dbe9820b103">std::upper_bound</a> (_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val, _Compare __comp) <li>template<typename _ForwardIterator , typename _Tp > _ForwardIterator <a class="el" href="a01186.html#ga9bf525d5276b91ff6441e27386034a75">std::upper_bound</a> (_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val) </ul> <hr/><a name="details" id="details"></a><h2>Detailed Description</h2> <p>These algorithms are variations of a classic binary search, and all assume that the sequence being searched is already sorted.</p> <p>The number of comparisons will be logarithmic (and as few as possible). The number of steps through the sequence will be logarithmic for random-access iterators (e.g., pointers), and linear otherwise.</p> <p>The LWG has passed Defect Report 270, which notes: <em>The proposed resolution reinterprets binary search. Instead of thinking about searching for a value in a sorted range, we view that as an important special case of a more general algorithm: searching for the partition point in a partitioned range. We also add a guarantee that the old wording did not: we ensure that the upper bound is no earlier than the lower bound, that the pair returned by equal_range is a valid range, and that the first part of that pair is the lower bound.</em></p> <p>The actual effect of the first sentence is that a comparison functor passed by the user doesn't necessarily need to induce a strict weak ordering relation. Rather, it partitions the range. </p> <hr/><h2>Function Documentation</h2> <a class="anchor" id="ga5957126a4e963070896e9c4ae5221820"></a><!-- doxytag: member="std::binary_search" ref="ga5957126a4e963070896e9c4ae5221820" args="(_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val)" --> <div class="memitem"> <div class="memproto"> <div class="memtemplate"> template<typename _ForwardIterator , typename _Tp > </div> <table class="memname"> <tr> <td class="memname">bool std::binary_search </td> <td>(</td> <td class="paramtype">_ForwardIterator </td> <td class="paramname"><em>__first</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">_ForwardIterator </td> <td class="paramname"><em>__last</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">const _Tp & </td> <td class="paramname"><em>__val</em> </td> </tr> <tr> <td></td> <td>)</td> <td></td><td></td> </tr> </table> </div> <div class="memdoc"> <p>Determines whether an element exists in a range. </p> <dl><dt><b>Parameters:</b></dt><dd> <table class="params"> <tr><td class="paramname">first</td><td>An iterator. </td></tr> <tr><td class="paramname">last</td><td>Another iterator. </td></tr> <tr><td class="paramname">val</td><td>The search term. </td></tr> </table> </dd> </dl> <dl class="return"><dt><b>Returns:</b></dt><dd>True if <em>val</em> (or its equivalent) is in [<em>first</em>,<em>last</em> ].</dd></dl> <p>Note that this does not actually return an iterator to <em>val</em>. For that, use std::find or a container's specialized find member functions. </p> <p>Definition at line <a class="el" href="a01045_source.html#l02665">2665</a> of file <a class="el" href="a01045_source.html">stl_algo.h</a>.</p> <p>References <a class="el" href="a01046_source.html#l00936">std::lower_bound()</a>.</p> </div> </div> <a class="anchor" id="ga9fc545c99c9b622c68d3b61ba04f326e"></a><!-- doxytag: member="std::binary_search" ref="ga9fc545c99c9b622c68d3b61ba04f326e" args="(_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val, _Compare __comp)" --> <div class="memitem"> <div class="memproto"> <div class="memtemplate"> template<typename _ForwardIterator , typename _Tp , typename _Compare > </div> <table class="memname"> <tr> <td class="memname">bool std::binary_search </td> <td>(</td> <td class="paramtype">_ForwardIterator </td> <td class="paramname"><em>__first</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">_ForwardIterator </td> <td class="paramname"><em>__last</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">const _Tp & </td> <td class="paramname"><em>__val</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">_Compare </td> <td class="paramname"><em>__comp</em> </td> </tr> <tr> <td></td> <td>)</td> <td></td><td></td> </tr> </table> </div> <div class="memdoc"> <p>Determines whether an element exists in a range. </p> <dl><dt><b>Parameters:</b></dt><dd> <table class="params"> <tr><td class="paramname">first</td><td>An iterator. </td></tr> <tr><td class="paramname">last</td><td>Another iterator. </td></tr> <tr><td class="paramname">val</td><td>The search term. </td></tr> <tr><td class="paramname">comp</td><td>A functor to use for comparisons. </td></tr> </table> </dd> </dl> <dl class="return"><dt><b>Returns:</b></dt><dd>True if <em>val</em> (or its equivalent) is in [<em>first</em>,<em>last</em> ].</dd></dl> <p>Note that this does not actually return an iterator to <em>val</em>. For that, use std::find or a container's specialized find member functions.</p> <p>The comparison function should have the same effects on ordering as the function used for the initial sort. </p> <p>Definition at line <a class="el" href="a01045_source.html#l02698">2698</a> of file <a class="el" href="a01045_source.html">stl_algo.h</a>.</p> <p>References <a class="el" href="a01046_source.html#l00936">std::lower_bound()</a>.</p> </div> </div> <a class="anchor" id="ga7941ea333830e800e324ae3e022e1f46"></a><!-- doxytag: member="std::equal_range" ref="ga7941ea333830e800e324ae3e022e1f46" args="(_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val, _Compare __comp)" --> <div class="memitem"> <div class="memproto"> <div class="memtemplate"> template<typename _ForwardIterator , typename _Tp , typename _Compare > </div> <table class="memname"> <tr> <td class="memname">pair<_ForwardIterator, _ForwardIterator> std::equal_range </td> <td>(</td> <td class="paramtype">_ForwardIterator </td> <td class="paramname"><em>__first</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">_ForwardIterator </td> <td class="paramname"><em>__last</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">const _Tp & </td> <td class="paramname"><em>__val</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">_Compare </td> <td class="paramname"><em>__comp</em> </td> </tr> <tr> <td></td> <td>)</td> <td></td><td></td> </tr> </table> </div> <div class="memdoc"> <p>Finds the largest subrange in which <em>val</em> could be inserted at any place in it without changing the ordering. </p> <dl><dt><b>Parameters:</b></dt><dd> <table class="params"> <tr><td class="paramname">first</td><td>An iterator. </td></tr> <tr><td class="paramname">last</td><td>Another iterator. </td></tr> <tr><td class="paramname">val</td><td>The search term. </td></tr> <tr><td class="paramname">comp</td><td>A functor to use for comparisons. </td></tr> </table> </dd> </dl> <dl class="return"><dt><b>Returns:</b></dt><dd>An pair of iterators defining the subrange.</dd></dl> <p>This is equivalent to </p> <div class="fragment"><pre class="fragment"> <a class="code" href="a01137.html#a9345a6e2e39831b4291cac2e52a15792" title="A convenience wrapper for creating a pair from two objects.">std::make_pair</a>(lower_bound(first, last, val, comp), upper_bound(first, last, val, comp)) </pre></div><p> but does not actually call those functions. </p> <p>Definition at line <a class="el" href="a01045_source.html#l02605">2605</a> of file <a class="el" href="a01045_source.html">stl_algo.h</a>.</p> <p>References <a class="el" href="a01053_source.html#l00171">std::advance()</a>, <a class="el" href="a01053_source.html#l00113">std::distance()</a>, <a class="el" href="a01046_source.html#l00936">std::lower_bound()</a>, and <a class="el" href="a01045_source.html#l02490">std::upper_bound()</a>.</p> </div> </div> <a class="anchor" id="gab12325ed36d6e07b06b3cbe74bec2845"></a><!-- doxytag: member="std::equal_range" ref="gab12325ed36d6e07b06b3cbe74bec2845" args="(_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val)" --> <div class="memitem"> <div class="memproto"> <div class="memtemplate"> template<typename _ForwardIterator , typename _Tp > </div> <table class="memname"> <tr> <td class="memname">pair<_ForwardIterator, _ForwardIterator> std::equal_range </td> <td>(</td> <td class="paramtype">_ForwardIterator </td> <td class="paramname"><em>__first</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">_ForwardIterator </td> <td class="paramname"><em>__last</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">const _Tp & </td> <td class="paramname"><em>__val</em> </td> </tr> <tr> <td></td> <td>)</td> <td></td><td></td> </tr> </table> </div> <div class="memdoc"> <p>Finds the largest subrange in which <em>val</em> could be inserted at any place in it without changing the ordering. </p> <dl><dt><b>Parameters:</b></dt><dd> <table class="params"> <tr><td class="paramname">first</td><td>An iterator. </td></tr> <tr><td class="paramname">last</td><td>Another iterator. </td></tr> <tr><td class="paramname">val</td><td>The search term. </td></tr> </table> </dd> </dl> <dl class="return"><dt><b>Returns:</b></dt><dd>An pair of iterators defining the subrange.</dd></dl> <p>This is equivalent to </p> <div class="fragment"><pre class="fragment"> <a class="code" href="a01137.html#a9345a6e2e39831b4291cac2e52a15792" title="A convenience wrapper for creating a pair from two objects.">std::make_pair</a>(lower_bound(first, last, val), upper_bound(first, last, val)) </pre></div><p> but does not actually call those functions. </p> <p>Definition at line <a class="el" href="a01045_source.html#l02543">2543</a> of file <a class="el" href="a01045_source.html">stl_algo.h</a>.</p> <p>References <a class="el" href="a01053_source.html#l00171">std::advance()</a>, <a class="el" href="a01053_source.html#l00113">std::distance()</a>, <a class="el" href="a01046_source.html#l00936">std::lower_bound()</a>, and <a class="el" href="a01045_source.html#l02490">std::upper_bound()</a>.</p> </div> </div> <a class="anchor" id="ga0ff3b53e875d75731ff8361958fac68f"></a><!-- doxytag: member="std::lower_bound" ref="ga0ff3b53e875d75731ff8361958fac68f" args="(_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val, _Compare __comp)" --> <div class="memitem"> <div class="memproto"> <div class="memtemplate"> template<typename _ForwardIterator , typename _Tp , typename _Compare > </div> <table class="memname"> <tr> <td class="memname">_ForwardIterator std::lower_bound </td> <td>(</td> <td class="paramtype">_ForwardIterator </td> <td class="paramname"><em>__first</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">_ForwardIterator </td> <td class="paramname"><em>__last</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">const _Tp & </td> <td class="paramname"><em>__val</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">_Compare </td> <td class="paramname"><em>__comp</em> </td> </tr> <tr> <td></td> <td>)</td> <td></td><td></td> </tr> </table> </div> <div class="memdoc"> <p>Finds the first position in which <em>val</em> could be inserted without changing the ordering. </p> <dl><dt><b>Parameters:</b></dt><dd> <table class="params"> <tr><td class="paramname">first</td><td>An iterator. </td></tr> <tr><td class="paramname">last</td><td>Another iterator. </td></tr> <tr><td class="paramname">val</td><td>The search term. </td></tr> <tr><td class="paramname">comp</td><td>A functor to use for comparisons. </td></tr> </table> </dd> </dl> <dl class="return"><dt><b>Returns:</b></dt><dd>An iterator pointing to the first element <em>not less than</em> <em>val</em>, or <a class="el" href="a01137.html#a6dd13574db6c6bda00577c2e1687960a" title="Return an iterator pointing to one past the last element of the initilizer_list.">end()</a> if every element is less than <em>val</em>.</dd></dl> <p>The comparison function should have the same effects on ordering as the function used for the initial sort. </p> <p>Definition at line <a class="el" href="a01045_source.html#l02394">2394</a> of file <a class="el" href="a01045_source.html">stl_algo.h</a>.</p> <p>References <a class="el" href="a01053_source.html#l00171">std::advance()</a>, and <a class="el" href="a01053_source.html#l00113">std::distance()</a>.</p> </div> </div> <a class="anchor" id="gabe324553abc3238696e8e2660bfa5c66"></a><!-- doxytag: member="std::lower_bound" ref="gabe324553abc3238696e8e2660bfa5c66" args="(_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val)" --> <div class="memitem"> <div class="memproto"> <div class="memtemplate"> template<typename _ForwardIterator , typename _Tp > </div> <table class="memname"> <tr> <td class="memname">_ForwardIterator std::lower_bound </td> <td>(</td> <td class="paramtype">_ForwardIterator </td> <td class="paramname"><em>__first</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">_ForwardIterator </td> <td class="paramname"><em>__last</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">const _Tp & </td> <td class="paramname"><em>__val</em> </td> </tr> <tr> <td></td> <td>)</td> <td></td><td></td> </tr> </table> </div> <div class="memdoc"> <p>Finds the first position in which <em>val</em> could be inserted without changing the ordering. </p> <dl><dt><b>Parameters:</b></dt><dd> <table class="params"> <tr><td class="paramname">first</td><td>An iterator. </td></tr> <tr><td class="paramname">last</td><td>Another iterator. </td></tr> <tr><td class="paramname">val</td><td>The search term. </td></tr> </table> </dd> </dl> <dl class="return"><dt><b>Returns:</b></dt><dd>An iterator pointing to the first element <em>not less than</em> <em>val</em>, or <a class="el" href="a01137.html#a6dd13574db6c6bda00577c2e1687960a" title="Return an iterator pointing to one past the last element of the initilizer_list.">end()</a> if every element is less than <em>val</em>. </dd></dl> <p>Definition at line <a class="el" href="a01046_source.html#l00936">936</a> of file <a class="el" href="a01046_source.html">stl_algobase.h</a>.</p> <p>References <a class="el" href="a01053_source.html#l00171">std::advance()</a>, and <a class="el" href="a01053_source.html#l00113">std::distance()</a>.</p> <p>Referenced by <a class="el" href="a01045_source.html#l02902">std::__merge_adaptive()</a>, <a class="el" href="a01045_source.html#l03015">std::__merge_without_buffer()</a>, <a class="el" href="a01045_source.html#l02665">std::binary_search()</a>, and <a class="el" href="a01045_source.html#l02543">std::equal_range()</a>.</p> </div> </div> <a class="anchor" id="gaac066ef92d4b5059d7609dbe9820b103"></a><!-- doxytag: member="std::upper_bound" ref="gaac066ef92d4b5059d7609dbe9820b103" args="(_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val, _Compare __comp)" --> <div class="memitem"> <div class="memproto"> <div class="memtemplate"> template<typename _ForwardIterator , typename _Tp , typename _Compare > </div> <table class="memname"> <tr> <td class="memname">_ForwardIterator std::upper_bound </td> <td>(</td> <td class="paramtype">_ForwardIterator </td> <td class="paramname"><em>__first</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">_ForwardIterator </td> <td class="paramname"><em>__last</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">const _Tp & </td> <td class="paramname"><em>__val</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">_Compare </td> <td class="paramname"><em>__comp</em> </td> </tr> <tr> <td></td> <td>)</td> <td></td><td></td> </tr> </table> </div> <div class="memdoc"> <p>Finds the last position in which <em>val</em> could be inserted without changing the ordering. </p> <dl><dt><b>Parameters:</b></dt><dd> <table class="params"> <tr><td class="paramname">first</td><td>An iterator. </td></tr> <tr><td class="paramname">last</td><td>Another iterator. </td></tr> <tr><td class="paramname">val</td><td>The search term. </td></tr> <tr><td class="paramname">comp</td><td>A functor to use for comparisons. </td></tr> </table> </dd> </dl> <dl class="return"><dt><b>Returns:</b></dt><dd>An iterator pointing to the first element greater than <em>val</em>, or <a class="el" href="a01137.html#a6dd13574db6c6bda00577c2e1687960a" title="Return an iterator pointing to one past the last element of the initilizer_list.">end()</a> if no elements are greater than <em>val</em>.</dd></dl> <p>The comparison function should have the same effects on ordering as the function used for the initial sort. </p> <p>Definition at line <a class="el" href="a01045_source.html#l02490">2490</a> of file <a class="el" href="a01045_source.html">stl_algo.h</a>.</p> <p>References <a class="el" href="a01053_source.html#l00171">std::advance()</a>, and <a class="el" href="a01053_source.html#l00113">std::distance()</a>.</p> <p>Referenced by <a class="el" href="a01045_source.html#l02902">std::__merge_adaptive()</a>, <a class="el" href="a01045_source.html#l03015">std::__merge_without_buffer()</a>, and <a class="el" href="a01045_source.html#l02543">std::equal_range()</a>.</p> </div> </div> <a class="anchor" id="ga9bf525d5276b91ff6441e27386034a75"></a><!-- doxytag: member="std::upper_bound" ref="ga9bf525d5276b91ff6441e27386034a75" args="(_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val)" --> <div class="memitem"> <div class="memproto"> <div class="memtemplate"> template<typename _ForwardIterator , typename _Tp > </div> <table class="memname"> <tr> <td class="memname">_ForwardIterator std::upper_bound </td> <td>(</td> <td class="paramtype">_ForwardIterator </td> <td class="paramname"><em>__first</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">_ForwardIterator </td> <td class="paramname"><em>__last</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">const _Tp & </td> <td class="paramname"><em>__val</em> </td> </tr> <tr> <td></td> <td>)</td> <td></td><td></td> </tr> </table> </div> <div class="memdoc"> <p>Finds the last position in which <em>val</em> could be inserted without changing the ordering. </p> <dl><dt><b>Parameters:</b></dt><dd> <table class="params"> <tr><td class="paramname">first</td><td>An iterator. </td></tr> <tr><td class="paramname">last</td><td>Another iterator. </td></tr> <tr><td class="paramname">val</td><td>The search term. </td></tr> </table> </dd> </dl> <dl class="return"><dt><b>Returns:</b></dt><dd>An iterator pointing to the first element greater than <em>val</em>, or <a class="el" href="a01137.html#a6dd13574db6c6bda00577c2e1687960a" title="Return an iterator pointing to one past the last element of the initilizer_list.">end()</a> if no elements are greater than <em>val</em>. </dd></dl> <p>Definition at line <a class="el" href="a01045_source.html#l02441">2441</a> of file <a class="el" href="a01045_source.html">stl_algo.h</a>.</p> <p>References <a class="el" href="a01053_source.html#l00171">std::advance()</a>, and <a class="el" href="a01053_source.html#l00113">std::distance()</a>.</p> </div> </div> </div> </div> <div id="nav-path" class="navpath"> <ul> <li class="footer">Generated by  <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </li> </ul> </div> </body> </html>