头文件 #include<algorithm>

  1. #include<iostream>
    #include<vector>
    #include<algorithm>
  2. #include<stdio.h>
  3. using namespace std;
  4. int main()
  5. {
  6. ///第一种
  7. int a[]={,,,,};
  8. int it1,it2;
  9. it1=lower_bound(a,a+,)-a;
  10. it2=upper_bound(a,a+,)-a;
  11. printf("%d %d\n",it1,a[it1]);
  12. // /*输出*/ 2 4
  13. printf("%d %d\n",it2,a[it2]);
  14. // /*输出*/ 4 6
  15. ///第二种
  16. vector<int>vec;
  17. vec.push_back(),vec.push_back();
  18. vec.push_back(),vec.push_back();
  19. vec.push_back();
  20. vector<int>::iterator it11,it22;
  21. it11=lower_bound(vec.begin(),vec.end(),);
  22. it22=upper_bound(vec.begin(),vec.end(),);
  23. it1=lower_bound(vec.begin(),vec.end(),)-vec.begin();
  24. it2=upper_bound(vec.begin(),vec.end(),)-vec.begin();
  25. printf("%d %d\n",it1,*it11);
  26. // /*输出*/ 2 4
  27. printf("%d %d\n",it2,*it22);
  28. // /*输出*/ 4 6
  29.   /// 若是数组内未出现的值 如3 则输出均为 2 4
  30. return ;
  31. }

结构体也可使用

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <stdio.h>
  4. #include <cstring>
  5. using namespace std;
  6. struct NODE
  7. {
  8. int x,y;
  9. bool operator<(const NODE& p)const{
  10. if(x==p.x) return y>p.y;
  11. return x<p.x;
  12. }
  13. }a[];
  14. int main()
  15. {
  16. int len=;
  17. a[len].x=, a[len++].y=;
  18. a[len].x=, a[len++].y=;
  19. a[len].x=, a[len++].y=;
  20. a[len].x=, a[len++].y=;
  21. sort(a,a+len);
  22. printf("sort:\n");
  23. for(int i=;i<len;i++)
  24. printf("%d %d\n",a[i].x,a[i].y);
  25. printf("\n");
  26.  
  27. int ind=lower_bound(a,a+len,(NODE){,})-a;
  28. printf("%d %d %d\n",ind,a[ind].x,a[ind].y);
  29.  
  30. return ;
  31. }

upper_bound() lower_bound() 用法的更多相关文章

  1. C/C++-STL中lower_bound与upper_bound的用法以及cmp函数

    转载于:http://blog.csdn.net/tjpuacm/article/details/26389441 不加比较函数的情况: int a[]={0,1,2,2,3}; printf(&qu ...

  2. hdu 4268 贪心+set lower_bound用法

    http://acm.hdu.edu.cn/showproblem.php?pid=4268 A想用手里的牌尽量多地覆盖掉B手中的牌.. 牌有h和w 问A手中的牌最多能覆盖B多少张牌 iterator ...

  3. C++ lower_bound/upper_bound用法解析

    1. 作用           lower_bound和upper_bound都是C++的STL库中的函数,作用差不多,lower_bound所返回的是第一个大于或等于目标元素的元素地址,而upper ...

  4. 关于lower_bound( )和upper_bound( )的常见用法

    lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begin,end,num):从数 ...

  5. lower_bound( )和upper_bound( )的常见用法

    lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begin,end,num):从数 ...

  6. STL 源码分析《5》---- lower_bound and upper_bound 详解

    在 STL 库中,关于二分搜索实现了4个函数. bool binary_search (ForwardIterator beg, ForwardIterator end, const T& v ...

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

    二分检索函数lower_bound()和upper_bound() 一.说明 头文件:<algorithm> 二分检索函数lower_bound()和upper_bound() lower ...

  8. Java实现 lower_bound() 和 upper_bound()

    Java实现 lower_bound() 和 upper_bound() lower_bound() 函数 lower_bound() 在 [begin, end) 进行二分查找,返回 大于或等于 t ...

  9. lower_bound( )和upper_bound( )怎么用嘞↓↓↓

    lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begin,end,num):从数 ...

随机推荐

  1. atlcomcli.h(1756): error C2338: CVarTypeInfo< char > cannot be compiled with /J or _CHAR_UNSIGNED fl

    我拿到一个VS的工程,用VS2010 编译 时提示: atlcomcli.h(1756): error C2338: CVarTypeInfo< char > cannot be comp ...

  2. delete 和 splice 删除数组中元素的区别

    delete 和 splice 删除数组中元素的区别 ` var arr1 = ["a","b","c","d"]; d ...

  3. 学习MFC创建界面

    原始学习文章地址: http://blog.csdn.net/chenyusiyuan/article/details/4744097 一.创建MFC 首先创建一个MFC对话框应用程序(Dialog- ...

  4. 1022 Digital Library (30 分)

    1022 Digital Library (30 分)   A Digital Library contains millions of books, stored according to thei ...

  5. SPOE网络供电

    本文摘自:https://wenku.baidu.com/view/929bd42daa00b52acec7ca3a.html 随着网络技术与应用的快速发展,网络前端设备的种类越来越多.架设越来越广泛 ...

  6. Java多态的实现机制是什么,写得非常好!

    作者:crane_practice www.cnblogs.com/crane-practice/p/3671074.html Java多态的实现机制是父类或接口定义的引用变量可以指向子类或实现类的实 ...

  7. Switch控制器

    比如目前有qq和百度两个网站,switch可以指定选择一个 Switch value是填写索引值,从0开始.也可以书去请求的名词比如,qq或者百度 填写0就是访问qq 填写1就是访问百度

  8. Ansible-随笔-7

    扩展Ansible的插件系统. 有的时候,如果Ansible内置的插件无法满足需求时,我们可以自己编写新插件. 以下情况下可以考虑开发新插件: 1.除Paramiko.本机SSH.Local.Winr ...

  9. Object相关方法

    const object1 = { a: 'somestring', b: 42, c: false }; console.log(Object.values(object1)); // expect ...

  10. 2018-2-13-win10-uwp-如何拖动一个TextBlock的文字到另一个TextBlock-

    title author date CreateTime categories win10 uwp 如何拖动一个TextBlock的文字到另一个TextBlock lindexi 2018-2-13 ...