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):从数 ...
随机推荐
- 类型转换、类型安全以及is和as的使用
class Program { static void Main(string[] args) { //1.类型转换 { //隐式转换:不需要转型,因为new返回一个Employee对象,而Objec ...
- 高级UI晋升之触摸事件分发机制(一)
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680 0. 前言 鉴于安卓分发机制较为复杂,故分为多个层次进行讲解,分别为基础篇.实践 ...
- 最小生成树--Prim及Kruskal
//prim算法#include<cstdio> #include<cmath> #include<cstring> #include<iostream> ...
- 6-MySQL-Ubuntu-操作数据表的基本操作(一)
注: SQL语句的关键字不区分大小写,如select 和Select都可以 (1) 查看当前使用的数据库; select database(); (2) 使用某数据库或切换到某数据库 use 数据库名 ...
- htons(), ntohl(), ntohs(),htons()这4个函数(摘)
在C/C++写网络程序的时候,往往会遇到字节的网络顺序和主机顺序的问题.这是就可能用到htons(), ntohl(), ntohs(),htons()这4个函数.网络字节顺序与本地字节顺序之间的转换 ...
- ArcGis相接面补节点c#
相接(Touch)面执行切割后 新面与原相接面会缺少公共节点. private void AddPointToTouchesPolygon(IFeatureCursor newFeatureCurso ...
- tomcat访问控制及站点部署
访问控制: 在访问tomcat服务器状态时,出现403错误. 解决方法: [root@localhost ~]# vim /usr/local/tomcat8/conf/tomcat-users.xm ...
- struts2注解返回json
Struts2使用注解方式返回Json数据 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Geek_Alex/article/details/788 ...
- Effective C++之条款1:视C++为一个语言联邦
C++中的sub-languages有如下四种: C Object-Oriented C++: (classes ,encapsulation(封装),inheritance(继承),polymo ...
- UartAssist串口调试工具
第一步安装UartAssist 第二步打开UartAssist 界面为 我们将我们的wifi模块连接电脑, 查看的端口号通过计算机管理设备管理器进行查看 根据要求发送数据 就可以了