upper_bound() lower_bound() 用法
头文件 #include<algorithm>
- #include<iostream>
#include<vector>
#include<algorithm>- #include<stdio.h>
- using namespace std;
- int main()
- {
- ///第一种
- int a[]={,,,,};
- int it1,it2;
- it1=lower_bound(a,a+,)-a;
- it2=upper_bound(a,a+,)-a;
- printf("%d %d\n",it1,a[it1]);
- // /*输出*/ 2 4
- printf("%d %d\n",it2,a[it2]);
- // /*输出*/ 4 6
- ///第二种
- vector<int>vec;
- vec.push_back(),vec.push_back();
- vec.push_back(),vec.push_back();
- vec.push_back();
- vector<int>::iterator it11,it22;
- it11=lower_bound(vec.begin(),vec.end(),);
- it22=upper_bound(vec.begin(),vec.end(),);
- it1=lower_bound(vec.begin(),vec.end(),)-vec.begin();
- it2=upper_bound(vec.begin(),vec.end(),)-vec.begin();
- printf("%d %d\n",it1,*it11);
- // /*输出*/ 2 4
- printf("%d %d\n",it2,*it22);
- // /*输出*/ 4 6
- /// 若是数组内未出现的值 如3 则输出均为 2 4
- return ;
- }
结构体也可使用
- #include <iostream>
- #include <algorithm>
- #include <stdio.h>
- #include <cstring>
- using namespace std;
- struct NODE
- {
- int x,y;
- bool operator<(const NODE& p)const{
- if(x==p.x) return y>p.y;
- return x<p.x;
- }
- }a[];
- int main()
- {
- int len=;
- a[len].x=, a[len++].y=;
- a[len].x=, a[len++].y=;
- a[len].x=, a[len++].y=;
- a[len].x=, a[len++].y=;
- sort(a,a+len);
- printf("sort:\n");
- for(int i=;i<len;i++)
- printf("%d %d\n",a[i].x,a[i].y);
- printf("\n");
- int ind=lower_bound(a,a+len,(NODE){,})-a;
- printf("%d %d %d\n",ind,a[ind].x,a[ind].y);
- return ;
- }
upper_bound() lower_bound() 用法的更多相关文章
- 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 ...
- hdu 4268 贪心+set lower_bound用法
http://acm.hdu.edu.cn/showproblem.php?pid=4268 A想用手里的牌尽量多地覆盖掉B手中的牌.. 牌有h和w 问A手中的牌最多能覆盖B多少张牌 iterator ...
- C++ lower_bound/upper_bound用法解析
1. 作用 lower_bound和upper_bound都是C++的STL库中的函数,作用差不多,lower_bound所返回的是第一个大于或等于目标元素的元素地址,而upper ...
- 关于lower_bound( )和upper_bound( )的常见用法
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begin,end,num):从数 ...
- lower_bound( )和upper_bound( )的常见用法
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begin,end,num):从数 ...
- STL 源码分析《5》---- lower_bound and upper_bound 详解
在 STL 库中,关于二分搜索实现了4个函数. bool binary_search (ForwardIterator beg, ForwardIterator end, const T& v ...
- 二分检索函数lower_bound()和upper_bound()
二分检索函数lower_bound()和upper_bound() 一.说明 头文件:<algorithm> 二分检索函数lower_bound()和upper_bound() lower ...
- Java实现 lower_bound() 和 upper_bound()
Java实现 lower_bound() 和 upper_bound() lower_bound() 函数 lower_bound() 在 [begin, end) 进行二分查找,返回 大于或等于 t ...
- lower_bound( )和upper_bound( )怎么用嘞↓↓↓
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begin,end,num):从数 ...
随机推荐
- 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 ...
- delete 和 splice 删除数组中元素的区别
delete 和 splice 删除数组中元素的区别 ` var arr1 = ["a","b","c","d"]; d ...
- 学习MFC创建界面
原始学习文章地址: http://blog.csdn.net/chenyusiyuan/article/details/4744097 一.创建MFC 首先创建一个MFC对话框应用程序(Dialog- ...
- 1022 Digital Library (30 分)
1022 Digital Library (30 分) A Digital Library contains millions of books, stored according to thei ...
- SPOE网络供电
本文摘自:https://wenku.baidu.com/view/929bd42daa00b52acec7ca3a.html 随着网络技术与应用的快速发展,网络前端设备的种类越来越多.架设越来越广泛 ...
- Java多态的实现机制是什么,写得非常好!
作者:crane_practice www.cnblogs.com/crane-practice/p/3671074.html Java多态的实现机制是父类或接口定义的引用变量可以指向子类或实现类的实 ...
- Switch控制器
比如目前有qq和百度两个网站,switch可以指定选择一个 Switch value是填写索引值,从0开始.也可以书去请求的名词比如,qq或者百度 填写0就是访问qq 填写1就是访问百度
- Ansible-随笔-7
扩展Ansible的插件系统. 有的时候,如果Ansible内置的插件无法满足需求时,我们可以自己编写新插件. 以下情况下可以考虑开发新插件: 1.除Paramiko.本机SSH.Local.Winr ...
- Object相关方法
const object1 = { a: 'somestring', b: 42, c: false }; console.log(Object.values(object1)); // expect ...
- 2018-2-13-win10-uwp-如何拖动一个TextBlock的文字到另一个TextBlock-
title author date CreateTime categories win10 uwp 如何拖动一个TextBlock的文字到另一个TextBlock lindexi 2018-2-13 ...