头文件:

#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的更多相关文章

  1. STL源码学习----lower_bound和upper_bound算法

    转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...

  2. 【刷题记录】 && 【算法杂谈】折半枚举与upper_bound 和 lower_bound

    [什么是upper_bound 和 lower_bound] 简单来说lower_bound就是你给他一个非递减数列[first,last)和x,它给你返回非递减序列[first, last)中的第一 ...

  3. STL之lower_bound和upper_bound

    ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...

  4. UVA 10474 大理石在哪 lower_bound

    题意:找输入的数在排完序之后的位置. 主要是lower_bound 函数的使用.它的作用是查找大于或者等于x的第一个位置. #include<cstdio> #include<alg ...

  5. [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 ...

  6. 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 ...

  7. [STL] lower_bound和upper_bound

    STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...

  8. STL lower_bound upper_bound binary-search

    STL中的二分查找——lower_bound .upper_bound .binary_search 二分查找很简单,原理就不说了.STL中关于二分查找的函数有三个lower_bound .upper ...

  9. vector的插入、lower_bound、upper_bound、equal_range实例

    对于这几个函数的一些实例以便于理解: #include <cstdlib> #include <cstdio> #include <cstring> #includ ...

  10. STL中的lower_bound和upper_bound的理解

    STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...

随机推荐

  1. BZOJ2783: [JLOI2012]树(树上前缀和+set)

    Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1215  Solved: 768[Submit][Status][Discuss] Descriptio ...

  2. 2018-09-06 Java实现英汉词典API初版发布在Maven

    在打算批量代码汉化工具 · Issue #86 · program-in-chinese/overview时, 发现没有现成的Java库实现英汉查询功能. 于是开此项目. 源码库: program-i ...

  3. 2018-08-14 中文代码之Spring Boot实现简单REST服务

    最终目标详见: 参考MSDN,试搞.NET类库标识符的翻译版 · Issue #54 · program-in-chinese/overview 此文仅为技术探索+原型搭建的第一小步. 源码库: 演示 ...

  4. docker研究-2

    容器和虚拟机都是一种虚拟化技术,两者的主要区别: 虚拟机占用资源多,启动慢,荣誉步骤多:而容器启动快,占用资源少,体积小.Docker 属于 Linux 容器的一种封装,提供简单易用的容器使用接口.它 ...

  5. Elasticsearch Elasticsearch入门指导

    Elasticsearch入门指导 By:授客 QQ:1033553122 1. 开启elasticsearch服务器 1 2. 基本概念 2 <1> 集群(Cluster) 2 < ...

  6. shell中的set、seq、eval、exec、&&和||

    一.set 查看set 帮助: bash -c "help set" 选项: -e:任何命令执行失败(非0 status)直接退出 -x: 打印执行过程的命令行.参数 +e:命令执 ...

  7. python datetime object 去除毫秒(microsecond)

    >>>import datetime >>>d = datetime.datetime.now().replace(microsecond=0) >>& ...

  8. web前端(14)—— JavaScript的数据类型,语法规范1

    编辑器选择 对js的编辑器选用,有很多,能对html编辑的,也能对js编辑,比如notepad++,visual studio code,webstom,atom,pycharm,sublime te ...

  9. ajax调用WebService实现数据库操作

    首先说下测试环境和思路: 前端收集数据转换成json格式传输到后端,处理并存入数据库 1.数据库操作: [WebMethod] public string InsertPoint(string dat ...

  10. Unity Editor 下创建Lua和Text文件

    预览 在Project视图中,扩展右键菜单,右键 – Create - Text File 创建一个Text文件,或者Lua文件. 关键点 获取当前选择的路径,以Assets路径开头 var sele ...