upper_bound和lower_bound用法(二分查找)

                     upper_bound和lower_bound用法(二分查找)

转载:https://blog.csdn.net/u011008379/article/details/50725670

   两个函数的用法类似,在一个左闭右开有序区间里进行二分查找,需要查找的值由第三个参数给出。 
    对于upper_bound来说,返回的是被查序列中第一个大于查找值的指针,也就是返回指向被查值>查找值的最小指针,lower_bound则是返回的是被查序列中第一个大于等于查找值的指针,也就是返回指向被查值>=查找值的最小指针。 
    不过除此之外,这两个函数还分别有一个重载函数,可以接受第四个参数。如果第四个参数传入greater<Type>(),其中Type改成对应类型,那么upper_bound则返回指向被查值<查找值的最小指针,lower_bound则返回指向被查值<=查找值的最小指针。 
    最后说一点使用的注意事项,先看这么一句话“ The elements in the range shall already be sorted according to this same criterion (operator< or comp), or at least partitioned with respect to val”(引用自http://www.cplusplus.com/reference/algorithm/upper_bound/)。简单来说,如果你用上述两个函数三个参数的那种形式,记得那个左闭右开的区间要为非递减的顺序,如果你给第四个参数传入greater<Type>(),则区间为非递增的顺序。

  • 附上一段代码,供大家快速检验使用(注释掉的是错误用法)
  • #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int seq1[] = {1, 2, 3, 3, 4, 5}, seq2[] = {9, 8, 7, 7, 6, 5};
    
    int main()
    {
        //cout<<upper_bound(seq1, seq1+6, 3, greater<int>()) - seq1<<endl;
        //cout<<lower_bound(seq1, seq1+6, 3, greater<int>()) - seq1<<endl;
        cout<<upper_bound(seq1, seq1+6, 3) - seq1<<endl;
        cout<<lower_bound(seq1, seq1+6, 3) - seq1<<endl;
    
        cout<<endl;
    
        cout<<upper_bound(seq2, seq2+6, 7, greater<int>()) - seq2<<endl;
        cout<<lower_bound(seq2, seq2+6, 7, greater<int>()) - seq2<<endl;
        //cout<<upper_bound(seq2, seq2+6, 7) - seq2<<endl;
        //cout<<lower_bound(seq2, seq2+6, 7) - seq2<<endl;
        return 0;
    }


   两个函数的用法类似,在一个左闭右开有序区间里进行二分查找,需要查找的值由第三个参数给出。 
    对于upper_bound来说,返回的是被查序列中第一个大于查找值的指针,也就是返回指向被查值>查找值的最小指针,lower_bound则是返回的是被查序列中第一个大于等于查找值的指针,也就是返回指向被查值>=查找值的最小指针。 
    不过除此之外,这两个函数还分别有一个重载函数,可以接受第四个参数。如果第四个参数传入greater<Type>(),其中Type改成对应类型,那么upper_bound则返回指向被查值<查找值的最小指针,lower_bound则返回指向被查值<=查找值的最小指针。 
    最后说一点使用的注意事项,先看这么一句话“ The elements in the range shall already be sorted according to this same criterion (operator< or comp), or at least partitioned with respect to val”(引用自http://www.cplusplus.com/reference/algorithm/upper_bound/)。简单来说,如果你用上述两个函数三个参数的那种形式,记得那个左闭右开的区间要为非递减的顺序,如果你给第四个参数传入greater<Type>(),则区间为非递增的顺序。

  • 附上一段代码,供大家快速检验使用(注释掉的是错误用法)
  • #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int seq1[] = {1, 2, 3, 3, 4, 5}, seq2[] = {9, 8, 7, 7, 6, 5};
    
    int main()
    {
        //cout<<upper_bound(seq1, seq1+6, 3, greater<int>()) - seq1<<endl;
        //cout<<lower_bound(seq1, seq1+6, 3, greater<int>()) - seq1<<endl;
        cout<<upper_bound(seq1, seq1+6, 3) - seq1<<endl;
        cout<<lower_bound(seq1, seq1+6, 3) - seq1<<endl;
    
        cout<<endl;
    
        cout<<upper_bound(seq2, seq2+6, 7, greater<int>()) - seq2<<endl;
        cout<<lower_bound(seq2, seq2+6, 7, greater<int>()) - seq2<<endl;
        //cout<<upper_bound(seq2, seq2+6, 7) - seq2<<endl;
        //cout<<lower_bound(seq2, seq2+6, 7) - seq2<<endl;
        return 0;
    }


猜你喜欢

转载自blog.csdn.net/xiao__jia__jia/article/details/80046983