STL lower_bound upper_bound 用法
1.lower_bound(begin,end,x)
返回第一个>=x的位置,找不到return .end()
2.upper_bound (begin,end,x)
返回第一个>x的位置,找不到return .end()
减掉begin得到下标
vector版
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
vector<int>a;
int b[];
int main(){ a.push_back();
a.push_back();
a.push_back();
a.push_back();
int p=lower_bound(a.begin(),a.end(),)-a.begin();
int q=lower_bound(a.begin(),a.end(),)-a.begin();
printf("%d %d\n",p,q);// 0 2 p=upper_bound(a.begin(),a.end(),)-a.begin();
q=upper_bound(a.begin(),a.end(),)-a.begin();
int r=upper_bound(a.begin(),a.end(),)-a.begin();
printf("%d %d %d\n",p,q,r);// 2 3 4 return ;
}
数组版
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
vector<int>a;
int b[]={,,,};
int main(){
int p=lower_bound(b,b+,)-b;
int q=lower_bound(b,b+,)-b;
printf("%d %d\n",p,q);// 0 2 p=upper_bound(b,b+,)-b;
q=upper_bound(b,b+,)-b;
int r=upper_bound(b,b+,)-b;
printf("%d %d %d\n",p,q,r);// 2 3 4 return ;
}
set (直接返回值)
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
using namespace std;
multiset<int>s;
int main(){
s.insert();
s.insert();
s.insert();
s.insert();
set<int>::iterator p=s.lower_bound();
set<int>::iterator q=s.lower_bound();
printf("%d %d\n",*p,*q);// 1 2
return ;
}
STL lower_bound upper_bound 用法的更多相关文章
- C++ lower_bound/upper_bound用法解析
1. 作用 lower_bound和upper_bound都是C++的STL库中的函数,作用差不多,lower_bound所返回的是第一个大于或等于目标元素的元素地址,而upper ...
- [STL]lower_bound&upper_bound
源码 lower_bound template <class ForwardIterator, class T> ForwardIterator lower_bound (ForwardI ...
- STL lower_bound upper_bound binary-search
STL中的二分查找——lower_bound .upper_bound .binary_search 二分查找很简单,原理就不说了.STL中关于二分查找的函数有三个lower_bound .upper ...
- stl lower_bound upper_bound binary_search equal_range
自己按照stl实现了一个: http://www.cplusplus.com/reference/algorithm/binary_search/ 这里有个注释,如何判断两个元素相同: Two e ...
- 鬼知道是啥系列之——STL(lower_bound(),upper_bound() )
引子,不明觉厉: 百度,渐入佳境: 头铁,入门到放弃: lower_bound(): 头文件: #include<algorithm>函数功能: 函数lower_bound()在f ...
- lower_bound && upper_bound
用lower_bound进行二分查找 ●在从小到大排好序的基本类型数组上进行二分查找. 这是二分查找的一种版本,试图在已排序的[first,last)中寻找元素value.如果[first,last ...
- STL中的二分查找———lower_bound,upper_bound,binary_search
关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...
- C++ STL lower_bound()和upper_bound()
lower_bound()和upper_bound()用法 1.在数组上的用法 假设a是一个递增数组,n是数组长度,则 lower_bound(a, a+n, x):返回数组a[0]~a[n-1]中, ...
- STL中的unique()和lower_bound ,upper_bound
unique(): 作用:unique()的作用是去掉容器中相邻元素的重复元素(数组可以是无序的,比如数组可以不是按从小到大或者从大到小的排列方式) 使用方法:unique(初始地址,末地址): 这里 ...
随机推荐
- Python基础教程(010)--第一个程序Hello Python
前言 会编写Hello Python源程序 内容 1,在桌面下,新建Python目录 2,在认识的Python目录下,新建一个HelloPython的文件 linux下: touch HellPyth ...
- RzPageControl(pagecontrol)
实现多标签的动态添加,切换,关闭 使用RzPageControl来实现多标签页使用菜单来打开标签页,通过标签页的caption来判断将标签页是否已经被打开过了.1.创建标签页,并判断是否是已经打开过的 ...
- PLSQL连接虚拟机中的Oracle数据库
下面这个连接对以后虚拟机安装非常有用!!! https://blog.csdn.net/lixin5456985/article/details/81670095
- 通过create-react-app从零搭建react环境
一. 快速开始: 全局安装脚手架: $ npm install -g create-react-app 通过脚手架搭建项目: $ create-react-app <项目名称> 开始项目: ...
- Android数字签名的学习(转)
转载地址:http://www.cnblogs.com/feisky/archive/2010/01/17/1650076.html 在Android系统中,所有安装到系统的应用程序都必有一个数字证书 ...
- Java泛型方法和构造函数
可以在方法声明中定义类型参数,它们在方法的返回类型之前的尖括号中指定.包含泛型方法声明的类型不必是通用类型.可以在非静态方法声明中使用为泛型类型指定的类型参数. 示例 以下代码显示如何为方法m1()定 ...
- ACM_ICPC_Team
题目: There are a number of people who will be attending ACM-ICPC World Finals. Each of them may be we ...
- quick'n'dirty poc for CVE-2013-1763 SOCK_DIAG bug in kernel 3.3-3.8
/* * quick'n'dirty poc for CVE-2013-1763 SOCK_DIAG bug in kernel 3.3-3.8 * bug found by Spender * po ...
- Oracle中常见的Hint(一)
Oracle中的Hint可以用来调整SQL的执行计划,提高SQL执行效率.下面分类介绍Oracle数据库中常见的Hint.这里描述的是Oracle11gR2中的常见Hint,Oracle数据库中各个版 ...
- 【总】.NET Core 2.0 详解
ASP.NET Core 认证与授权[7]:动态授权 雨夜朦胧 2017-11-24 11:21 阅读:7063 评论:19 ASP.NET Core 认证与授权[6]:授权策略是怎么执行的? 雨夜朦 ...