#include<algorithm>//头文件
//标准形式
lower_bound(int* first,int* last,val);
upper_bound(int* first,int* last,val);
lower_bound( )和upper_bound( )都是利用二分查找在一个排好序的从小到大的数组中进行查找,时间复杂度为O(logn)。
函数lower_bound()在first和last中的前闭后开区间,进行二分查找。返回从first开始的第一个大于或等于val的元素的地址。如果所有元素都小于val,则返回last的地址。
函数upper_bound()在first和last中的前闭后开区间,进行二分查找。返回从first开始的第一个大于val的元素的地址。如果所有元素都小于或等于val,则返回last的地址。
通常用法:
T* FoundAddr = std::lower_bound(&pBegin[0], &pBegin[Count], Value);
若数组排序是从大到小,可以通过重载lower_bound( )和upper_bound( )函数实现功能。
完毕!
有任何问题欢迎留言讨论~