#include <iostream>     // std::cout
#include <algorithm>    // std::upper_bound
#include <vector>       // std::vector
using namespace std;
//以普通函数的方式定义查找规则
bool mycomp(int i, int j) { return i > j; }
//以函数对象的形式定义查找规则
class mycomp2 {
public:bool operator()(const int& i, const int& j) {return i > j;}
};
int main() {int a[5] = { 1,2,3,4,5 };//从 a 数组中找到第一个大于 3 的元素int *p = upper_bound(a, a + 5, 3);cout << "*p = " << *p << endl;vector<int> myvector{ 4,5,3,1,2 };// 纠正C语言中文网的错误,此处是第一个满足规则的元素,不是违背//根据 mycomp2 规则,从 myvector 容器中找到第一个满足 mycomp2 规则的元素vector<int>::iterator iter = upper_bound(myvector.begin(), myvector.end(), 3, mycomp2());/*lambda函数可写为:vector<int>::iterator iter = upper_bound(myvector.begin(), myvector.end(), 3, [](int a,int b){// a 是参数3return a>b;// myvector中第一个小于3的元素就是1
});*/cout << "*iter = " << *iter;return 0;
}

upper_bound()函数的使用相关推荐

  1. C++ lower_bound 与 upper_bound 函数

    头文件: #include  <algorithm> 二分查找的函数有 3 个: 参考:C++ lower_bound 和upper_bound lower_bound(起始地址,结束地址 ...

  2. C++中lower_bound函数和upper_bound函数

    STL中关于二分查找的函数有三个lower_bound .upper_bound .binary_search .这三个函数都运用于有序区间(当然这也是运用二分查找的前提),下面记录一下这两个函数. ...

  3. lower_bound()/upper_bound()函数(C++)

    文章目录 1.lower_bound()/upper_bound()函数简单介绍 2.lower_bound()/upper_bound()函数分析 3.lower_bound()/upper_bou ...

  4. C语言bound函数,C++中lower_bound函数和upper_bound函数

    STL中关于二分查找的函数有三个lower_bound .upper_bound .binary_search .这三个函数都运用于有序区间(当然这也是运用二分查找的前提),下面记录一下这两个函数. ...

  5. C++ lower_bound() upper_bound() 函数用法详解(深入了解,一文学会)

    find().find_if().search() 等.值得一提的是,这些函数的底层实现都采用的是顺序查找(逐个遍历)的方式,在某些场景中的执行效率并不高.例如,当指定区域内的数据处于有序状态时,如果 ...

  6. lower_bound()和upper_bound( )函数详解

    lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的.两个函数的用法类似,在一个左闭右开的有序区间里进行二分查找,需要查找的值由第三个参数给出 ...

  7. (C++)用upper_bound函数取代自己写的二分查找

    int a[maxn];int j = upper_bound(a+i+1,a+n,(long long)a[i]*p)-a; 以上代码的作用是 在a[i+1]~a[n-1]找到第一个大于a[i]*p ...

  8. upperbound找不到_lower_bound()函数和upper_bound()函数,以及二分查找

    lower_bound(): 默认版本 template ForwardIterator lower_bound (ForwardIterator first, ForwardIterator las ...

  9. 二分搜索:lower_bound 与 upper_bound 函数

    基本用法: lower_bound 和upper_bound需要用在一个有序数组和容器中. (1)lower_bound (first,last,val)用来寻找在数组和容器[first,last)的 ...

最新文章

  1. PHP开发者应了解的24个库
  2. Android_设备隐私获取,忽略6.0权限管理
  3. abap submit 的使用方法
  4. 【Linux】一步一步学Linux——last命令(98)
  5. Mysql 中根据条件排序获取排名
  6. linux高亮查找关键字
  7. CSDN查看旧版主页的方法
  8. 服务器改win7 性能,性能大改善,微软Win10版本1909即将推出,网友为何坚持win7?...
  9. 《天天数学》连载22:一月二十二日
  10. JavaScript之jQuery够用即可(each循环、位置偏移量和大小、克隆外层标签、嵌套返回顶部、集体绑定事件)
  11. Java多线程——线程范围内共享变量和ThreadLocal
  12. vmstat 命令详解
  13. nifi集群_NiFi安装(单机+集群)
  14. 3C认证与电源PFC
  15. 黑客语言——Ruby
  16. MySQL Flashback拯救手抖党
  17. 【论文笔记】RS--基于卫星遥感的中国县级尺度小麦估产研究
  18. 深入理解connect by levelxxx
  19. 普通用户获取详细执行计划需要哪些权限
  20. Java获取12306余票信息(一)

热门文章

  1. java.sql.SQLException: ORA-01436: 用户数据中的 CONNECT BY 循环
  2. AutoJs4.4.1免费版快速接通vscode进行调试脚本
  3. PHP设计模式之简单工厂模式*解读
  4. NAND FLASH芯片R/B引脚上拉电阻的作用和阻值选择
  5. 自律的人生,更自由。
  6. Altium AD20设置过孔盖油,默认规则盖油,无网络普通过孔盖油
  7. 三里人家夫子的运营秘诀
  8. UEFI PCD分析
  9. Android APP Camera2应用(02)预览流程
  10. python的短路计算