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(初始地址,末地址): 这里 ...
随机推荐
- PHP closedir() 函数
打开一个目录,读取它的内容,然后关闭: <?php$dir = "/images/"; // Open a directory, and read its contentsi ...
- UNP学习第13章 守护进程和inetd超级服务器
Unix系统中的syslogd守护进程通常由某个系统初始化脚本启动,而且在系统工作期间一直运行. 源自Berkeley的syslogd实现在启动时执行以下步骤. (1)读取配置文件.通常为/etc/s ...
- flask中app.py: error: invalid choice: 'insert'........的问题
在flask中,分为两种操作方式,url指向函数或者终端指向函数,最终目的就是让函数执行,触发方式不同.终端执行更加安全, 另外如果在终端执行的时候出现 : 就是说明 insert 没有在@ mana ...
- python正常时间和unix时间戳相互转换的方法
python正常时间和unix时间戳相互转换的方法 本文实例讲述了python正常时间和unix时间戳相互转换的方法.分享给大家供大家参考.具体分析如下: 这段代码可以用来转换常规时间格式为unix时 ...
- z-index只能用在定位元素上
弄了很久才突然想到z-index只能用在被定位的元素上. 定位的时候要注意给父级定位 在ie7里有问题的部分
- python学习笔记:模块——os模块(操作文件/目录)
import os os.rename(old,new) #重命名文件或目录 os.remove(f)#删除文件 os.mkdir('china/beijing') #创建文件夹 os.makedir ...
- linux替换rm命令,防止误删
1. 在/home/username/ 目录下新建一个目录,命名为:.trash 2.. 在/home/username/tools/目录下,新建一个shell文件,命名为: remove.sh #! ...
- java中的三大特性
java的三大特性是封装.继承.多态: 继承是OOD(面向对象设计)为了更好的建模,编程的时候是OOP(面向对象编程)提高代码的复用性.这里有个注意点:一个类只有一个直接的父类,但不是只有一个父类. ...
- MySQL总结02
sql优化 Insert使用批量 查询不要使用*, MySQ需要先查出表里的所有字段,再进行匹配 字符串的查询条件要带引号,否则可能不走索引 备份及恢复 备份 mysqldump -uroot -pp ...
- Mysq sql语句教程
mysql管理命令 show databases; 显示服务器上当前所有的数据库 use 数据库名称; 进入指定的数据库 show tables; 显示当前数据库中所有的数据表 d ...