头文件:

#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. Android Studio Git 分支使用实践

    新公司有些项目是用的 Git,以前公司都是 svn,为了练手 Git,我个人 APP 用到了,但是仅简单的 git pull/push 的使用,并未用到 Git 精髓,只有当项目中用到,才会紧迫去全面 ...

  2. 深入理解Java虚拟机04--类结构文件

    一.程序存储格式 统一的程序存储格式:不同平台的虚拟机于所有平台都统一使用程序存储格式——字节码(ByteCode); Java 虚拟机不关心 Class 文件的来源,而只和“Class文件" ...

  3. GPA简介

    GPA(Graphics Performance Analyzers)是Intel公司提供的一款免费的跨平台性能分析工具. 填写e-mail.name和country并提交后,就会收到一封有专属下载链 ...

  4. celery任务进程关闭

    方法1: ps auxww|grep 方法2: Ctrl+C 方法3: celery multi 管理 celery multi start w1 -A proj -l info celery mul ...

  5. C和C++头文件大全

    C.传统 C++ #include <assert.h> //设定插入点#include <ctype.h>  //字符处理#include <errno.h>   ...

  6. spring4笔记----使用装配注入合作者Bean的三种方式

    no :不自动装配 byName :id(name)与setter方法去set前缀,并小写首字母后同名的Bean完成注入,如有多个匹配则抛异常 byType :spring容器找全部bean,如果找到 ...

  7. SQL Server -- 回忆笔记(一):初见数据库

    SQL Server知识点回忆篇(一):初见数据库 1.  主键 primary key    唯一标识, 不会重复的值才会用来当做主键使用. 表可以没有主键,但建议每张表都有主键. 2.  数据冗余 ...

  8. margin的两个有趣现象:margin合并和margin塌陷

    margin合并 当两个元素在垂直方向并列,分别设置margin值时会发生一个margin合并的现象 举个例子,有两个div,垂直并列,box1设置margin-bottom:20px,box2设置m ...

  9. shell read变量的读入

    shell变量的输入: shell变量除了可以直接赋值或脚本传参外,还可以使用read命令从标准输入获取,read为bash内置命令,可以通过help read查看帮助. 语法格式: read [参数 ...

  10. FastCGI Error Number: 5 (0x80070005).

    在访问网站的时候,出现了以上这个错误: 在网上搜了很多方法,归纳起来就如下几种: 1, 网站安全狗]的安全策略问题 解决方案: 主动防御/禁止IIS执行程序 添加"php\php-cgi.e ...