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 ...
随机推荐
- mysql数据库表操作-表的主键索引和普通索引
数据库索引就象书的目录一样,如果在字段上建立了索引,那么以索引列为查询条件时可以加快查询数据的速度.查询数据库,按主键查询是最快的,每个表只能有一个主键列,但是可以有多个普通索引列,主键列要求列的所有 ...
- Nginx 配置下载附件让浏览器提示用户是否保存
Nginx配置下载附件让浏览器提示用户是否保存 by:授客 QQ:1033553122 测试环境 nginx-1.10.0 问题描述: 前端页面,IE11浏览器下请求下载附件模板,针对xls ...
- Python 关于类函数设计的一点总结
关于类函数设计的一点总结 by:授客 QQ:1033553122 代码1 #!/usr/bin/env python #-*-encoding:utf-8-*- __author__ = 'shouk ...
- 直接通过Binder的onTransact完成跨进程通信
1.具体代码: 服务端实现: public class IPCService extends Service { private static final String DESCRIPTOR = &q ...
- <自动化测试方案_2>第二章、自动化测试是什么?(What)
第二章.自动化测试是什么?(What) 自动化测试是相对于手工测试而言:通过脚本自动去执行测试用例,从而代替人完成测试工作. 自动化测试相对手工测试优缺点 测试方式 优点 缺点 手工测试 1,完整的对 ...
- Testlink1.9.17使用方法(第八章 测试执行/报告BUG)
第八章 测试执行/报告BUG QQ群交流:585499566 把他们放到一起,是因为报告bug是在执行的过程中同步进行的——即执行用例的过程中一旦发现bug我们需要立即把其报告到我们的bug管理系统r ...
- leetcode-69.x的平方根
leetcode-69.x的平方根 Points 二分查找 牛顿迭代 题意 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保 ...
- ORACLE导入大量数据的两种方式比较
不管是开发还是测试,工作中经常需要去批量新增测试数据,但是大量数据的新增速度有时候让我们苦不堪言,下面通过两种方式完成oracle数据的批量新增,比较两种方式的效率. 第一种方式:采用工具导入sql文 ...
- 企业建立成功 DevOps 模式所需应对的5个挑战
[编者按]本文作者为 Kevin Goldberg,主要介绍要想成功部署 DevOps 模式,企业所需应对的5大挑战与问题.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 要给 DevOps ...
- backup是个相对论
工作互备,是很多团队领导者都关注的事情.显然,当一项任务由两个(甚至两个以上的人)来完成,当任务交付使用后出现问题时,不会因为其中某一个成员的缺席而导致问题一时处理不了.如果某个任务只是由一个人来担当 ...