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)算法返回一个非递减序列[first, last)中第一个大于val的位置。

     lower_bound和upper_bound如下图所示:

upper_bound和lower_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;
}



相关文章:

  • 2021-10-23
  • 2021-06-30
  • 2022-01-05
  • 2021-12-14
  • 2022-12-23
  • 2021-12-09
  • 2021-09-23
  • 2021-07-14
猜你喜欢
  • 2022-12-23
  • 2021-07-30
  • 2021-07-07
  • 2021-06-22
  • 2021-10-09
相关资源
相似解决方案