STL中的二分查找———lower_bound,upper_bound,binary_search

     关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search 。

     STL中关于二分查找的函数有三个lower_bound 、upper_bound 、binary_search 。这三个函数都运用于有序区间(当然这也是运用二分查找的前提)。

     Tips:1.在检索前,应该用sort函数对数组进行从小到大排序。

        2.使用以上函数时必须包含头文件:#include < algorithm >     

     

一、lower_bound() 函数

       函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置

       举例如下:

  一个数组number序列为:4,10,11,30,69,70,96,100.设要插入数字3,9,111.pos为要插入的位置的下标

  则:

    pos = lower_bound( number, number + 8, 3) - number,pos = 0.即number数组的下标为0的位置。

    pos = lower_bound( number, number + 8, 9) - number, pos = 1,即number数组的下标为1的位置(即10所在的位置)。

    pos = lower_bound( number, number + 8, 111) - number, pos = 8,即number数组的下标为8的位置(但下标上限为7,所以返回最后一个元素的下一个素)。

  所以,要记住:函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的!!~

  返回查找元素的第一个可安插位置,也就是“元素值>=查找值”的第一个元素的位置

       

       测试代码如下:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <functional>
 4 #include <vector>
 5 
 6 using namespace std;
 7 
 8 
 9 int main()
10 {
11     const int VECTOR_SIZE = 8 ;
12 
13     // Define a template class vector of int
14     typedef vector<int > IntVector ;
15 
16     //Define an iterator for template class vector of strings
17     typedef IntVector::iterator IntVectorIt ;
18 
19     IntVector Numbers(VECTOR_SIZE) ;
20 
21     IntVectorIt start, end, it, location ;
22 
23     // Initialize vector Numbers
24     Numbers[0] = 4 ;
25     Numbers[1] = 10;
26     Numbers[2] = 11 ;
27     Numbers[3] = 30 ;
28     Numbers[4] = 69 ;
29     Numbers[5] = 70 ;
30     Numbers[6] = 96 ;
31     Numbers[7] = 100;
32 
33     start = Numbers.begin() ;   // location of first
34                                 // element of Numbers
35 
36     end = Numbers.end() ;       // one past the location
37                                 // last element of Numbers
38 
39     // print content of Numbers
40     cout << "Numbers { " ;
41     for(it = start; it != end; it++)
42         cout << *it << " " ;
43     cout << " }
" << endl ;
44 
45     // return the first location at which 10 can be inserted
46     // in Numbers
47     location = lower_bound(start, end, 1) ;
48 
49     cout << "First location element 10 can be inserted in Numbers is: "
50         << location - start<< endl ;
51 }
View Code

二、upper_bound()函数

  函数upper_bound()返回的在前闭后开区间查找的关键字的上界,

  如一个数组number序列1,2,2,4.upper_bound(2)后,返回的位置是3(下标)也就是4所在的位置,同样,如果插入元素大于数组中全部元素,返回的是last。(注意:此时数组下标越界!!)

  返回查找元素的最后一个可安插位置,也就是“元素值>查找值”的第一个元素的位置

  测试代码如下:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <functional>
 4 #include <vector>
 5 using namespace std;
 6 
 7 void main()
 8 {
 9     const int VECTOR_SIZE = 8 ;
10 
11     // Define a template class vector of int
12     typedef vector<int, allocator<int> > IntVector ;
13 
14     //Define an iterator for template class vector of strings
15     typedef IntVector::iterator IntVectorIt ;
16 
17     IntVector Numbers(VECTOR_SIZE) ;
18 
19     IntVectorIt start, end, it, location, location1;
20 
21     // Initialize vector Numbers
22     Numbers[0] = 4 ;
23     Numbers[1] = 10;
24     Numbers[2] = 10 ;
25     Numbers[3] = 30 ;
26     Numbers[4] = 69 ;
27     Numbers[5] = 70 ;
28     Numbers[6] = 96 ;
29     Numbers[7] = 100;
30 
31     start = Numbers.begin() ;   // location of first
32                                 // element of Numbers
33 
34     end = Numbers.end() ;       // one past the location
35                                 // last element of Numbers
36 
37     // print content of Numbers
38     cout << "Numbers { " ;
39     for(it = start; it != end; it++)
40         cout << *it << " " ;
41     cout << " }
" << endl ;
42 
43     //return the last location at which 10 can be inserted
44     // in Numbers
45     location = lower_bound(start, end, 9) ;
46     location1 = upper_bound(start, end, 10) ;
47 
48     cout << "Element 10 can be inserted at index "
49         << location - start<< endl ;
50      cout << "Element 10 can be inserted at index "
51         << location1 - start<< endl ;
52 }
View Code

三、binary_search

函数模版: 
  template<typename T>
  int  binary_search (T arr[],  int size,  T target) ;

参数说明:
  T: 模版参数
  arr :  数组首地址
  size: 数组元素个数
  T target : 需要查找的值
  返回值: 如果数组中找到target, 返回其下标,否则返回 -1
  要求数组元素顺序非递减

   binary_search试图在已排序的[first,last)中寻找元素value,若存在就返回true,若不存在则返回false。返回单纯的布尔值也许不能满足需求,而lower_bound、upper_bound能提供额外的信息。事实上由源码可知binary_search便是利用lower_bound求出元素应该出现的位置,然后再比较该位置的值与value的值。

                                                                                                                                                                  

原文地址:https://www.cnblogs.com/yaoyueduzhen/p/4531766.html