lower_bound
头文件:
#include<algorithm>
作用:
查找第一个大于或等于给定数的元素或位置
在从小到大的排列数组中
注意注意:
是排列好的,
一般都是从小到大,
但从大到小也可以,
只不过做法与常规的从小到大的不太一样
查找有序区间中第一个大于或等于某给定值的元素的位置
其中排序规则可以通过二元关系来表示
代码举例:
1.针对容器
(1).得到具体的元素:
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> v;
int main()
{
for(int i = ;i < ;i++)
v.push_back( * i);//注意:此时v中的元素本身就是有序的
vector<int>::iterator it = lower_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()
{
for(int i = ;i < ;i++)
v.push_back(i * );//注意:此时v中的元素本身就是有序的
int pos = lower_bound(v.begin(),v.end(),) - v.begin();
printf("%d\n",pos);
return ;
}
这时候返回pos就是所查找元素的位置,下标,
这里查找到的元素应该是4在容器中的下标是1,
所以输出pos的结果就是1 2.针对数组
返回元素:
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int a[] = {,,,};
int *it = lower_bound(a,a+,);
printf("%d\n",*it);
return ;
}
返回位置:
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int a[] = {,,,};//注意:初始化中的元素本身就是有序的
int pos = lower_bound(a,a+,)-a;
printf("%d\n",pos);
return ;
}
以上都是针对从小到大排列好的
------------------------------------------------------------------------------------------------------------------------
下面是从大到小排列好的
函数:
greater<int>()
头文件:
#include<functional>
假如说像上边一样元素为2 4 6 8,
逆序则是8 6 4 2,
那么求距离3最近表示的是与3最近的小于等于3的元素,
输出结果则是元素2了,
代码如下:
#include<cstdio>
#include<algorithm>
#include<vector>
#include<functional>
using namespace std;
vector<int> v;
int main()
{
for(int i = ;i > ;i--)
v.push_back(i * );
vector<int>::iterator it = lower_bound(v.begin(),v.end(),,greater<int>());
printf("%d\n",*it);
return ;
}
说明,要查找的有序序列必须是合法的,已经被排序的序列。
lower_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 ...
- UVA 10474 大理石在哪 lower_bound
题意:找输入的数在排完序之后的位置. 主要是lower_bound 函数的使用.它的作用是查找大于或者等于x的第一个位置. #include<cstdio> #include<alg ...
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- 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在容器内的时候,从 ...
随机推荐
- Activiti实现流程自由跳转
import org.activiti.engine.ProcessEngine; import org.activiti.engine.TaskService; import org.activit ...
- SAP MM MM17里不能修改物料主数据'Purchasing Value Key'字段值?
SAP MM MM17里不能修改物料主数据'Purchasing Value Key'字段值? 记得在D项目上线之前数据导入系统之后,业务提出一些物料采购视图里的’Purchasing value k ...
- SOD框架--系统概要
SOD框架(源PDF.NETE框架)系统概要介绍 --核心三大功能(S,O,D): SQL-MAP XML SQL config and Map DAL SQL Map Entity ORM OQL( ...
- 21.Odoo产品分析 (三) – 人力资源板块(2) – 工时表(1)
查看Odoo产品分析系列--目录 工时表是一个用来管理员工工作时间和出勤的模块.当需要计算员工的工作时间,并将这些时间对应到项目或者顾客上时,"工时表"就会起到一个非常好的作用. ...
- Android Touch事件传递机制 二:单纯的(伪生命周期) 这个清楚一点
转载于:http://blog.csdn.net/yuanzeyao/article/details/38025165 在前一篇文章中,我主要讲解了Android源码中的Touch事件的传递过程,现在 ...
- JS辨别访问浏览器判断是android还是ios系统
function isIOSOrAndroid() { var browser = { versions: function() { var u = navigator.userAgent, app ...
- webpack项目轻松混用css module
前言 本文讲述css-loader开启css模块功能之后,如何与引用的npm包中样式文件不产生冲突. 比如antd-mobilenpm包的引入.在不做特殊处理的前提下,样式文件将会被转译成css mo ...
- jQuery 实现文字不停闪烁效果
使用jQuery实现的小效果:文字不停地闪烁. var flag = true; var text= $('#blink').text(); // blink是需要闪烁的元素id function b ...
- Java读取.properties配置文件
一.介绍 Properties文件在Java中主要为配置文件,文件类型为:.properties,格式为文本文件,内容格式为"键=值" 二.读取 这里我采用的是getResourc ...
- scrapy中pipeline的一点综合知识
初次学习scrapy ,觉得spider代码才是最重要的,越往后学,发现pipeline中的代码也很有趣, 今天顺便把pipeline中三种储存方法写下来,算是对自己学习的一点鼓励吧,也可以为后来者的 ...