头文件:

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

  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. STL之lower_bound和upper_bound

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

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

  6. [STL] lower_bound和upper_bound

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

  7. STL lower_bound upper_bound binary-search

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

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

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

  9. STL中的lower_bound和upper_bound的理解

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

  10. STL 源码分析《5》---- lower_bound and upper_bound 详解

    在 STL 库中,关于二分搜索实现了4个函数. bool binary_search (ForwardIterator beg, ForwardIterator end, const T& v ...

随机推荐

  1. @meda媒体查询

    定义和使用 使用 @media 查询,你可以针对不同的媒体类型定义不同的样式. @media 可以针对不同的屏幕尺寸设置不同的样式,特别是如果你需要设置设计响应式的页面,@media 是非常有用的. ...

  2. SAP FI配置步骤

    http://blog.sina.com.cn/s/blog_8eda1a620100uwzj.html No. 配置对象 事务代码 配置内容 路径 备注 1 定义公司 OX15 企业结构>定义 ...

  3. 照葫芦画瓢系列之Java --- Maven的配置

    一.Maven仓库分类 Maven中,仓库只分为两类:本地仓库和远程仓库.当Maven根据坐标寻找构件的时候,它首先去查看本地仓库,如果本地仓库有此构件,则直接使用,如果本地仓库不存在此构件,或者需要 ...

  4. 简单易用的堡垒机系统—Teleport

    简单易用的堡垒机系统-Teleport 官方文档:http://teleport.eomsoft.net/doc#!1  一.Teleport介绍 Teleport是触维软件推出的一款简单易用的堡垒机 ...

  5. eclipse中web项目没有run on server

    刷新web项目,项目->右击->Properties->Project Facets,选择Java和Dynamic Web Module.点击Apply and Close,再次运行 ...

  6. Android--小游戏

    GitHub:https://github.com/vinieo/game 功能描述   “猜小球”是一个简单的愉悦身心的小游戏,它的功能结构如图a-1所示. 构建开发环境 在开发本游戏时,首先需要下 ...

  7. 关于WPF中TextBox使用SelectAll无效的问题的解决办法

    1.首先保证你设置的SelectionBrush不是透明的颜色或者和背景色相同 2.在使用SelectAll之前要保证Textox以及获取到焦点. this.textbox.SelectionBrus ...

  8. pyinstaller打包错误 UnicodeDecodeError: 'gbk' codec can't decode byte 0xab in position 160:

    注:我的博客原本在CSDN,现转到博客园,图片采用以前的图片,并没有盗图. 在将.py文件打包时,出现了下列错误 >>C:\Users\小呆\PycharmProjects\pycharm ...

  9. Powershell测试端口状态

    function Test-Port { Param([string]$ComputerName,$port = 5985,$timeout = 1000) try { $tcpclient = Ne ...

  10. python 制作一对一聊天

    用到的参考资料 https://blog.csdn.net/jia666666/article/details/81624550 https://blog.csdn.net/jia666666/art ...