lower_bound() 与 upper_bound()

#include <iostream>
#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最多能插入数组point的哪个位置
 printf("%d\n",tmp);
 tmp = lower_bound(point, point + 5, 7) - point;////按从小到大,7最少能插入数组point的哪个位置
 printf("%d\n",tmp);
 return 0;
}
output:
4
2

lower_bound()函数返回范围内第一个

不小于(大于或等于)

指定val的值。

如果序列中的值都小于val,则返回last.

序列应该已经有序!

upper_bound()函数返回范围内第一个 大于 指定val的值。

如果序列中的值都小于val,则返回last.

序列应该已经有序!

时间: 2024-08-07 17:00:17

lower_bound() 与 upper_bound()的相关文章

二分lower_bound()与upper_bound()的运用

<span style="color:#6633ff;">/* G - 二分 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Status Description Given n points (1 dimensional) and q segments, you have to find the number of points that lie in each o

STL之std::set、std::map的lower_bound和upper_bound函数使用说明

由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊的函数,lower_bound.upper_bound.equal_range. 原型如下: iterator lower_bound (const value_type& val) const; iterator upper_bound (const value_type& val) con

[STL] lower_bound和upper_bound

STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于等于值val的位置. ForwardIter upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)算法返回一个非递减序列[first, last)中第一个大于val的位置.

二分检索函数lower_bound()和upper_bound()

二分检索函数lower_bound()和upper_bound() 一.说明 头文件:<algorithm> 二分检索函数lower_bound()和upper_bound() lower_bound():找到大于等于某值的第一次出现upper_bound():找到大于某值的第一次出现必须从小到大排序后才能用 内部查找方式为二分查找,二分查找必定需要排序 返回值为地址 二.代码及结果 1 /* 2 二分检索函数lower_bound()和upper_bound() 3 lower_bound(

stl map中的lower_bound和 upper_bound

map中的lower_bound和upper_bound的意思其实很简单,就两句话: map::lower_bound(key):返回map中第一个大于或等于key的迭代器指针 map::upper_bound(key):返回map中第一个大于key的迭代器指针 所以,理解这两个函数请不要按照字面意义思考太复杂,因为仅仅是不小于(lower_bound)和大于(upper_bound)这么简单. 看两个msdn里的例子 1 // map_upper_bound.cpp 2 // compile

[转] STL源码学习----lower_bound和upper_bound算法

http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html PS: lower_bound of value 就是最后一个 < value 或者第一个 = value的位置 upper_bound of value 就是第一个 > value的位置 lower_bound的意思是一段相等的序列的头(闭)和尾(开)的位置 STL中关于二分查找的函数有三个lower_bound .upper_bound .binary_search

STL algorithm算法lower_bound和upper_bound(31)

lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class ForwardIterator, class T> ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val); custom (2) template <class Forw

Leetcode 34 Search for a Range (二分搜索 lower_bound和upper_bound)

Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order ofO(logn). If the target is not found in the array, return [-1, -1]. For example, Given [5, 7, 7

lower_bound和upper_bound算法

STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于等于值val的位置. ForwardIter upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)算法返回一个非递减序列[firs

STL源码学习----lower_bound和upper_bound算法

STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于等于值val的位置. ForwardIter upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)算法返回一个非递减序列[firs