Binary search (operating on sorted ranges):

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 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  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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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:
1
2
3
4
5
6
7
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:
1
2
3
4
5
6
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));
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值