wordpress清理网站缓存,免费小程序模板,做风能的网站,搜索引擎优化论文3000字二分检索函数lower_bound()和upper_bound() 一、说明 头文件#xff1a;algorithm 二分检索函数lower_bound()和upper_bound() lower_bound()#xff1a;找到大于等于某值的第一次出现upper_bound()#xff1a;找到大于某值的第一次出现必须从小到大排序后才能用 内… 二分检索函数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 #include iostream
10 #include algorithm
11 #include string
12 using namespace std;
13
14
15 int main(){
16
17 int a[]{9,2,4,5,10,7,30};
18 sort(a,a7);//省略掉排序规则的形式默认从小到大
19 //sort(a,a7,lessint());//用系统的排序规则从小到大
20 //sort(a,a7,greaterint());//用系统的排序规则从大到小
21 for(int i0;i7;i){
22 couta[i] a[i]endl;
23 }
24 coutendl;
25 /*
26 这个lower_bound要从小到大排序才正确
27 如果是从大到小或者不排序还是输出的从小到大排序好后的位置
28 */
29 int *plower_bound(a,a7,2);//用lower_bound找大于等于2的第一次出现
30 coutpendl;
31 cout*pendl;
32 int *p1upper_bound(a,a7,2);//用upper_bound找大于等于2的第一次出现
33 coutp1endl;
34 cout*p1endl;
35
36
37
38 return 0;
39 } 转载于:https://www.cnblogs.com/Renyi-Fan/p/6961035.html