C++的快排与upper_bound()和lower_bound()
来源:互联网 发布:网络传销犯罪典型案例 编辑:程序博客网 时间:2024/06/06 01:55
函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置
举例如下:
一个数组number序列为:4,10,11,30,69,70,96,100.设要插入数字3,9,111.pos为要插入的位置的下标
则
pos = lower_bound( number, number + 8, 3) - number,pos = 0.即number数组的下标为0的位置。
pos = lower_bound( number, number + 8, 9) - number, pos = 1,即number数组的下标为1的位置(即10所在的位置)。
pos = lower_bound( number, number + 8, 111) - number, pos = 8,即number数组的下标为8的位置(但下标上限为7,所以返回最后一个元素的下一个元素)。
所以,要记住:函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的!!~
返回查找元素的第一个可安插位置,也就是“元素值>=查找值”的第一个元素的位置
函数upper_bound()返回的在前闭后开区间查找的关键字的上界,如一个数组number序列1,2,5,7.upper_bound(2)后,返回的位置是2(下标)也就是5所在的位置,同样,如果插入元素大于数组中全部元素,返回的是last。(注意:此时数组下标越界!!)
返回查找元素的最后一个可安插位置,也就是“元素值>查找值”的第一个元素的位置
快排:::
重点
#include <stdlib.h>
int compi(const void *a, const void *b)
{
const int *p = (int *)a;
const int *q = (int *)b;
return *p - *q;
}
int compd(const void *a, const void *b)
{
const int *p = (int *)a;
const int *q = (int *)b;
return (*p - *q) * (-1);
}
int main()
{
int a[1000];
int i, len, type;
while(scanf("%d %d", &len, &type) != EOF)
{
for(i = 0; i < len; i ++)
{
scanf("%d", &a[i]);
}
switch(type)
{
case 1 :
//递增排序
qsort(a, len, sizeof(a[0]), compi);
break;
case 2 :
//递减排序
qsort(a, len, sizeof(a[0]), compd);
break;
}
if(type == 1)
{
printf("递增排序结果:\n");
}else
{
printf("递减排序结果:\n");
}
for(i = 0; i < len; i ++)
{
printf("%d ", a[i]);
}
printf("\n");
}
return 0;
}
- C++的快排与upper_bound()和lower_bound()
- [C++] upper_bound和lower_bound
- C/C++-STL中lower_bound与upper_bound的用法
- lower_bound()与upper_bound()的区别
- lower_bound与upper_bound的用法
- upper_bound和lower_bound的用法
- lower_bound和upper_bound的区别
- upper_bound()和lower_bound()的使用
- lower_bound和upper_bound的用法
- upper_bound和lower_bound的用法
- C++primer学习:关于upper_bound和lower_bound的探究.
- C中的二分函数:upper_bound()与lower_bound()
- c语言实现lower_bound和upper_bound
- C++---------lower_bound upper_bound和unique函数
- c++lower_bound 函数和upper_bound函数
- lower_bound()与upper_bound()函数的使用
- lower_bound与upper_bound还有fill的使用
- 二分lower_bound()与upper_bound()的运用
- applicationContext.xml 配置
- 快速排序
- Validate Binary Search Tree
- 冒泡和选择(从大到小排序)
- 火狐浏览器设置背景图片
- C++的快排与upper_bound()和lower_bound()
- 支付宝sign 与 mysign 返回值不一致 的 bug
- Android之File操作权限
- Android如何通过shareduserid获取系统权限
- vector模板类的成员函数
- 在vc中使用sqlite3数据库
- 初学java
- 弹出框无论如何点都做同样的事情
- Codeforces Round#250 D. The Child and Zoo(并差集)