Binary search (operating on sorted ranges):

来源:互联网 发布:淘宝萌羽 编辑:程序博客网 时间:2024/06/06 21:44

std::lower_bound

<algorithm>
template <class ForwardIterator, class T>  ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last,                                const T& value );template <class ForwardIterator, class T, class Compare>  ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last,                                const T& value, Compare comp );
Return iterator to lower bound
Returns an iterator pointing to the first element in the sorted range [first,last) which does not compare less thanvalue. The comparison is done using either operator< for the first version, or comp for the second.

For the function to yield the expected result, the elements in the range shall already be ordered according to the same criterion (operator< or comp).

Unlike upper_bound, this function returns an iterator to the element also if it compares equivalent to value and not only if it compares greater.

The behavior of this function template is equivalent to:
123456789101112131415
template <class ForwardIterator, class T>  ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last, const T& value ){  ForwardIterator it;  iterator_traits<ForwardIterator>::difference_type count, step;  count = distance(first,last);  while (count>0)  {    it = first; step=count/2; advance (it,step);    if (*it<value)                   // or: if (comp(*it,value)), for the comp version      { first=++it; count-=step+1;  }    else count=step;  }  return first;}

std::upper_bound

<algorithm>
template <class ForwardIterator, class T>  ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last,                                const T& value );template <class ForwardIterator, class T, class Compare>  ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last,                                const T& value, Compare comp );
Return iterator to upper bound
Returns an iterator pointing to the first element in the sorted range [first,last) which compares greater than value. The comparison is done using either operator< for the first version, or comp for the second.

For the function to yield the expected result, the elements in the range shall already be ordered according to the same criterion (operator< or comp).

Unlike lower_bound, this function does not return an iterator to the element if it compares equivalent to value, but only if it compares strictly greater.

The behavior of this function template is equivalent to:
123456789101112131415
template <class ForwardIterator, class T>  ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last, const T& value ){  ForwardIterator it;  iterator_traits<ForwardIterator>::difference_type count, step;  count = distance(first,last);  while (count>0)  {    it = first; step=count/2; advance (it,step);    if (!(value<*it))                 // or: if (!comp(value,*it)), for the comp version      { first=++it; count-=step+1;  }    else count=step;  }  return first;}

std::equal_range

<algorithm>
template <class ForwardIterator, class T>  pair<ForwardIterator,ForwardIterator>    equal_range ( ForwardIterator first, ForwardIterator last, const T& value );template <class ForwardIterator, class T, class Compare>  pair<ForwardIterator,ForwardIterator>    equal_range ( ForwardIterator first, ForwardIterator last, const T& value,                  Compare comp );
Get subrange of equal elements
Returns the bounds of the largest subrange that includes all the elements of the [first,last) with values equivalent to value.

The comparison is performed using either operator< for the first version, or comp for the second: A value, a, is considered equivalent to another, b, when (!(a<b) && !(b<a)) or (!comp(a,b) && !comp(b,a)) 
If value is not equivalent to any value in the range, the subrange returned has a length of zero, with both iterators pointing to the nearest value greater than value, if any, or to last, if value compares greater than all the elements in the range.

For the function to yield the expected result, the elements in the range shall already be ordered according to the same criterion (operator< or comp).

The behavior of this function template is equivalent to:
1234567
template <class ForwardIterator, class T>  pair<ForwardIterator,ForwardIterator>    equal_range ( ForwardIterator first, ForwardIterator last, const T& value ){  ForwardIterator it = lower_bound (first,last,value);  return make_pair ( it, upper_bound(it,last,value) );}

std::binary_search

<algorithm>
template <class ForwardIterator, class T>  bool binary_search ( ForwardIterator first, ForwardIterator last,                       const T& value );template <class ForwardIterator, class T, class Compare>  bool binary_search ( ForwardIterator first, ForwardIterator last,                       const T& value, Compare comp );
Test if value exists in sorted array
Returns true if an element in the range [first,last) is equivalent to value, and false otherwise.

The comparison is performed using either operator< for the first version, or comp for the second: A value, a, is considered equivalent to another, b, when (!(a<b) && !(b<a)) or (!comp(a,b) && !comp(b,a)) 
For the function to yield the expected result, the elements in the range shall already be ordered according to the same criterion (operator< or comp).

The behavior of this function template is equivalent to:
123456
template <class ForwardIterator, class T>  bool binary_search ( ForwardIterator first, ForwardIterator last, const T& value ){  first = lower_bound(first,last,value);  return (first!=last && !(value<*first));}

12345678910111213141516171819202122232425262728
// binary_search example#include <iostream>#include <algorithm>#include <vector>using namespace std;bool myfunction (int i,int j) { return (i<j); }int main () {  int myints[] = {1,2,3,4,5,4,3,2,1};  vector<int> v(myints,myints+9);                         // 1 2 3 4 5 4 3 2 1  // using default comparison:  sort (v.begin(), v.end());  cout << "looking for a 3... ";  if (binary_search (v.begin(), v.end(), 3))    cout << "found!\n"; else cout << "not found.\n";  // using myfunction as comp:  sort (v.begin(), v.end(), myfunction);  cout << "looking for a 6... ";  if (binary_search (v.begin(), v.end(), 6, myfunction))    cout << "found!\n"; else cout << "not found.\n";  return 0;}


Output:
looking for a 3... found!looking for a 6... not found.

原创粉丝点击