二分检索函数lower_bound()和upper_bound()
一、说明
头文件:<algorithm>
二分检索函数lower_bound()和upper_bound()
lower_bound():找到大于等于某值的第一次出现upper_bound():找到大于某值的第一次出现必须从小到大排序后才能用 内部查找方式为二分查找,二分查找必定需要排序返回值为地址
二、代码及结果
1 /* 2 二分检索函数lower_bound()和upper_bound() 3 lower_bound():找到大于等于某值的第一次出现 4 upper_bound():找到大于某值的第一次出现 5 必须从小到大排序后才能用 6 内部查找方式为二分查找,二分查找必定需要排序 7 返回值为地址 8 */ 9 #include10 #include 11 #include 12 using namespace std;13 14 15 int main(){16 17 int a[]={ 9,2,4,5,10,7,30};18 sort(a,a+7);//省略掉排序规则的形式,默认从小到大 19 //sort(a,a+7,less ());//用系统的排序规则,从小到大 20 //sort(a,a+7,greater ());//用系统的排序规则,从大到小 21 for(int i=0;i<7;i++){22 cout< <<" "<<&a[i]<