upper_bound
头文件:
#include<algorithm>
作用:
查找第一个大于给定数的元素或位置
在从小到大的排序数组中,
1.容器
(1).返回元素
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> v;
int main()
{
int a[] = {,,,,};
for(int i = ;i < ;i++)
v.push_back(a[i]);
vector<int>::iterator it = upper_bound(v.begin(),v.end(),);
printf("%d\n",*it);
return ;
}
(2).返回位置
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> v;
int main()
{
int a[] = {,,,,};
for(int i = ;i < ;i++)
v.push_back(a[i]);
int pos = upper_bound(v.begin(),v.end(),)-v.begin();
printf("%d\n",pos);
return ;
}
2.数组
(1).返回元素
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int a[] = {,,,,};
int *it = upper_bound(a,a+,);
printf("%d\n",*it);
return ;
}
(2).返回位置
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int a[]={,,,,};
int pos = upper_bound(a,a+,)-a;
printf("%d\n",pos);
return ;
}
在从小到大的排序数组中,
upper_bound( begin,end,num,greater<type>() )
其他用法与upper_bound(从小到大的那个)相似
说明,要查找的有序序列必须是合法的,已经被排序的序列。
upper_bound的更多相关文章
- STL源码学习----lower_bound和upper_bound算法
转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...
- 【刷题记录】 && 【算法杂谈】折半枚举与upper_bound 和 lower_bound
[什么是upper_bound 和 lower_bound] 简单来说lower_bound就是你给他一个非递减数列[first,last)和x,它给你返回非递减序列[first, last)中的第一 ...
- STL_lower_bound&upper_bound用法
ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...
- STL之lower_bound和upper_bound
ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- [STL] lower_bound和upper_bound
STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...
- STL lower_bound upper_bound binary-search
STL中的二分查找——lower_bound .upper_bound .binary_search 二分查找很简单,原理就不说了.STL中关于二分查找的函数有三个lower_bound .upper ...
- vector的插入、lower_bound、upper_bound、equal_range实例
对于这几个函数的一些实例以便于理解: #include <cstdlib> #include <cstdio> #include <cstring> #includ ...
- STL中的lower_bound和upper_bound的理解
STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...
- STL 源码分析《5》---- lower_bound and upper_bound 详解
在 STL 库中,关于二分搜索实现了4个函数. bool binary_search (ForwardIterator beg, ForwardIterator end, const T& v ...
随机推荐
- elementUI vue 编辑中的input的验证残留清除
当使用编辑的时候, 假如上次的验证没通过, 报红了, 下次再点击编辑的时候还会报红,因此要清除验证残留, 方式有两种: this.$refs["from"].resetFields ...
- 洛谷P3209 [HNOI2010]PLANAR(2-SAT)
题目描述 若能将无向图G=(V,E)画在平面上使得任意两条无重合顶点的边不相交,则称G是平面图.判定一个图是否为平面图的问题是图论中的一个重要问题.现在假设你要判定的是一类特殊的图,图中存在一个包含所 ...
- 洛谷P1337 [JSOI2004]平衡点 / 吊打XXX(模拟退火)
题目描述 如图:有n个重物,每个重物系在一条足够长的绳子上.每条绳子自上而下穿过桌面上的洞,然后系在一起.图中X处就是公共的绳结.假设绳子是完全弹性的(不会造成能量损失),桌子足够高(因而重物不会垂到 ...
- Git 常用命令及操作总结
Git常用命令及操作总结 By:授客 QQ:1033553122 利用TortoiseGit克隆源码库到本地 1.安装TortoiseGit 2.打开Git,进入到源码库,点击图示红色选框框选按钮,弹 ...
- Keystone, Start, Failed to Load Bson
If you have installed the Keystone.js, and properly installed mongodb, but when tried to start the k ...
- vue 构建项目遇到的请求本地json问题
在本地测试的json没有问题,但是打包后,发现json 的路径不对了,变成了绝对路径 解决方法: 建立的json文件需要放置 根目录/static下.如项目名/static/data.json,这边 ...
- SQL SERVER启动步骤
第一步 从注册表读取SQL SERVER启动信息 (1)Audit Level:设置SQL SERVER是否记录用户登陆信息 Login Mode:设置SQL SERVER登陆类型是只接受windo ...
- c/c++ 友元基本概念
友元基本概念: 1,把一个一般函数声明为一个类的友元函数 2,把一个类A的某几个成员函数声明为某个类B的友元函数 3,把一个类A声明为一个类B的友元类. 友元的作用:可以访问类B里所有的成员变量和成员 ...
- c strlen和sizeof详解
用双引号定义并且声明的时候明确指定数组大小的话,sizeof就会返回指定的大小,不会自动加1: char str2[10] = "hello c"; printf("st ...
- 编写脚本实现DHCP服务与DHCP中继自动化执行
编写脚本实现DHCP服务与DHCP中继自动化执行 本脚本是在liunx搭建DHCP服务器以及DHCP中继服务器实验环境下实现的https://www.cnblogs.com/yuzly/p/10539 ...