#include <iostream>
#include <algorithm>//必须包含的头文件
using namespace std;
int main(){
int point[10] = {1,3,7,7,9};
return 0;
}
output:
4
2
#include <algorithm>//必须包含的头文件
using namespace std;
int main(){
int point[10] = {1,3,7,7,9};
int tmp = upper_bound(point, point + 5, 7) - point;//按从小到大,
最后一个小于等于7点位置
printf("%d\n",tmp);tmp = lower_bound(point, point + 5, 7) - point;////按从小到大,
最后一个小于7的位置
printf("%d\n",tmp);return 0;
}
output:
4
2
C++ upper_bound与lower_bound用法
本文通过一个简单的C++示例介绍了如何使用upper_bound和lower_bound算法来查找数组中特定元素的位置。这两个函数在标准库<algorithm>中定义,能够高效地找到指定元素在已排序数组中的插入点。
9万+

被折叠的 条评论
为什么被折叠?



