lower_bound()&&upper_bound()

来源:互联网 发布:java class 命名规范 编辑:程序博客网 时间:2024/05/16 15:14
lower_bound的:  出处:http://www.cnblogs.com/o8le/archive/2012/03/21/2410716.html

lower_bound的headfile是algorithm.

lower_bound的工作原理就是二分查找了。

lower_bound的作用:

lower_bound的返回值减去数组的地址就是

要查找的元素在数组中的位置。

即:Pos = lower_bound(a, a+10, 3)-a;

那么Pos就是在数组a[10]中的位置了。

 

 

 

upper_bound()与lower_bound()使用方法

出处:http://blog.sina.com.cn/s/blog_62582b7e0100eyqz.html

#include
#include //必须包含的头文件
using namespace std;

int main(){
int point[10] = {1,3,7,7,9};
int tmp = upper_bound(point, point + 5, 7) -point;//按从小到大,7最多能插入数组point的哪个位置
printf("%d\n",tmp);
tmp = lower_bound(point, point + 5, 7) -point;////按从小到大,7最少能插入数组point的哪个位置
printf("%d\n",tmp);
return 0;
}

output:

4

2

 

 

原创粉丝点击