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):从数 ...
随机推荐
- Openstack组件部署 — Nova_Install and configure a compute node
目录 目录 前文列表 Prerequisites 先决条件 Install and configure a compute node Install the packages Edit the etc ...
- 14、testng.xml 设置用例执行顺序
目录如下: TestGroup.java 代码如下: package com.testng.cn; import org.testng.annotations.*; import static org ...
- XStream环境设置
为Windows 2000/XP设置路径: 假设安装在c:Program Filesjavajdk目录: 在“我的电脑”右键单击并选择“属性”. 在“高级”选项卡下单击“环境变量”按钮. 现在,改变“ ...
- Ubuntu建立和删除用户
添加用户: sudo adduser xxx 这样的命令会在home目录下添加一个帐号 sudo useradd xxx 仅仅是添加用户, 不会在home目录添加帐号 删除用户: 在普通用户下:sud ...
- C语言typedef
#include <stdio.h> //基本类型 typedef int MyInt; //可以对typedef产生的类型名二次起别名 typedef MyInt MyInt2; // ...
- centos7 sshd 安全设置
ssh 的安全机制 1.SSH之所以能够保证安全,原因在于它采用了非对称加密技术(RSA)加密了所有传输的数据. 2.传统的网络服务程序,如FTP等在网络上用明文传送数据.用户帐号和用户口令,很容 ...
- 【学术篇】luogu3768 简单的数学题(纯口胡无代码)
真是一道"简单"的数学题呢~ 反演题, 化式子. \[ ans=\sum_{i=1}^n\sum_{j=1}^nijgcd(i,j) \\ =\sum_{i=1}^n\sum_{j ...
- zeromq protobuf例子
https://github.com/AifiHenryMa/zeromq_protocolbuffer_demo https://github.com/protocolbuffers/protobu ...
- leetcood学习笔记-202-快乐数
题目描述: 方法一:比较笨的办法,根据题意,如果变成1返回True,如果出现重复返回False 看到下面有位朋友用的是dict,我用了list,两个都跑了一下似乎list快一点? class Solu ...
- Android android studio常用的一些快捷键以及常用权限
android studio的常用快捷键:一.打印log:1.输入logt回车: 自动生成TAG的全局变量:private static final String TAG = "MainA ...