zoukankan      html  css  js  c++  java
  • 13-STL-二分查找

    STL中提供-二分查找算法(binary_search lower_bound upper_bound equal_range

     

     STL包含四种不同的二分查找算法,binary_search    lower_bound  upper_bound   equal_range.他们作用的range是已sorted。

    binary_search试图在已排序的[first, last)中寻找元素value。如果[first, last)内有等价于value的元素,它会返回true,否则返回false,它不返回查找位置。

    lower_bound它试图在已排序的[first,last)中寻找元素value。如果[first, last)具有等价于value的元素,lower_bound返回一个iterator指向其中第一个元素。如果没有这样的元素存在,它便返回假设这样的元素存在的话,会出现的位置。即指向第一个不小于value的元素。如果value大于[first, last)的任何一个元素,则返回last。

    upper_bound它试图在已排序的[first,last)中寻找value,返回可安插value的最后一个合适的位置。如果value存在,lower_bound 返回的是指向该元素的iterator。相较之下upper_bound并不这么做,它返回value可被安插的最后一个合适位置。如果value存在,那么它返回的iterator将指向value的下一个位置,而非value自身。

    equal_range的返回值本质上结合了lower_bound和upper_bound两者的返回值。其返回值是一对iterator i 和 j , 其中i是value可安插的第一个位置,j则是value可安插的最后一个位置。可以推演出:[i,j)中的每个元素都等价于value,而且[i, j)是[first, last)之中符合上述性质的一个最大子区间。  算法lower_bound返回该range的第一个iterator, 算法upper_bound返回该range的past-the-end iterator,算法equal_range则是以pair的形式将两者都返回。

     
    [cpp] view plain copy
     
    1. #include <algorithm>  
    2. #include <iostream>  
    3. #include <iterator>  
    4. #include <vector>  
    5. using namespace std;  
    6. int main()  
    7. {  
    8.     vector<int> v;  
    9.     vector<int>::iterator iter;  
    10.     pair<vector<int>::iterator, vector<int>::iterator> vecpair;  
    11.   
    12.   
    13.     for(int i = 1; i<= 20; i++) {  
    14.         v.push_back(i%6);  
    15.     }  
    16.     sort(v.begin(), v.end());  
    17.     cout << "array: " << endl << "  ";  
    18.     copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));  
    19.     cout << endl << endl;  
    20.   
    21.   
    22.     /*  lower_bound */  
    23.     cout << "lower_bound function, value = 3: " << endl;  
    24.     iter = lower_bound(v.begin(), v.end(), 3);  
    25.     cout << "  [first, iter] = ";  
    26.     copy(v.begin(), iter, ostream_iterator<int>(cout, " "));  
    27.     cout << endl;  
    28.     cout << "  [iter, last] = ";  
    29.     copy(iter, v.end(), ostream_iterator<int>(cout, " "));  
    30.     cout << endl << endl;  
    31.   
    32.   
    33.     /*  upper_bound */  
    34.     cout << "upper_bound function, value = 3: " << endl;  
    35.     iter = upper_bound(v.begin(), v.end(), 3);  
    36.     cout << "  [first, iter] = ";  
    37.     copy(v.begin(), iter, ostream_iterator<int>(cout, " "));  
    38.     cout << endl;  
    39.     cout << "  [iter, last] = ";  
    40.     copy(iter, v.end(), ostream_iterator<int>(cout, " "));  
    41.     cout << endl << endl;  
    42.   
    43.   
    44.     /*  equal_range */  
    45.     cout << "euqual_range function value = 3: " << endl;  
    46.     vecpair = equal_range(v.begin(), v.end(), 3);  
    47.   
    48.   
    49.     cout << " [vecpair->first, vecpair->second] = ";  
    50.     copy(vecpair.first, vecpair.second, ostream_iterator<int>(cout, " "));  
    51.     cout << endl << endl;  
    52.   
    53.   
    54.     /*  binary_search */  
    55.     cout << "binary_search function value = 3: " << endl;  
    56.     cout << "3 is " << (binary_search(v.begin(), v.end(), 3) ? "": "not ") << " in array" << endl;  
    57.     cout << endl;  
    58.   
    59.   
    60.     /*  binary_search */  
    61.     cout << "binary_search function value = 6: " << endl;  
    62.     cout << "6 is " << (binary_search(v.begin(), v.end(), 6) ? "": "not ") << " in array" << endl;  
    63.     cout << endl;  
    64. }  
    65.   
    66.   
    67. ./bsearch   
    68. array:   
    69.   0 0 0 1 1 1 1 2 2 2 2 3 3 3 4 4 4 5 5 5   
    70.   
    71.   
    72. lower_bound function, value = 3:   
    73.   [first, iter] = 0 0 0 1 1 1 1 2 2 2 2   
    74.   [iter, last] = 3 3 3 4 4 4 5 5 5   
    75.   
    76.   
    77. upper_bound function, value = 3:   
    78.   [first, iter] = 0 0 0 1 1 1 1 2 2 2 2 3 3 3   
    79.   [iter, last] = 4 4 4 5 5 5   
    80.   
    81.   
    82. euqual_range function value = 3:   
    83.  [vecpair->first, vecpair->second] = 3 3 3   
    84.   
    85.   
    86. binary_search function value = 3:   
    87. 3 is  in array  
    88.   
    89.   
    90. binary_search function value = 6:   
    91. 6 is not  in array  
  • 相关阅读:
    归并排序
    希尔排序
    字符串操作
    引用
    直接插入排序
    变量赋值
    C#中关于公共类的使用
    关于SQL中Between语句查询日期的问题
    用户控件 与 重写控件 的区别
    什么是命名空间,为什么要使用命名空间?
  • 原文地址:https://www.cnblogs.com/zhumengdexiaobai/p/8613156.html
Copyright © 2011-2022 走看看