二分函数 lower_bound()
这篇博客说是STL源码。。。。
https://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html
头文件 algorithm
1、lower_bound()
沿用百度百科,这个函数有两个版本可用,lower_bound(*array, len, key), lower_bound(*array, len, key, cmp)
- 第一个函数函数查找的是,在左闭右开的区间内,第一个大于或等于 key 的数,返回值是该值的地址!
- 第二个函数就后面加了个自定义比较函数
所以一般用这个函数的时候要先排序,自己可以试试不排序。
#include <iostream>
#include <algorithm> using namespace std; int main()
{
int a[1005], n;
cin >> n;
for(int i = ;i < n;++i)
cin >> a[i];
sort(a, a + n); int num;
while(cin >> num && num)
{
int *it = lower_bound(a, a + n, num);
cout << "地址 " << it << endl;
cout << "位置 " << it - a + << endl;
if(it - a - n)
cout << "值 " << *it << endl;
//这里看了一些代码都没有判断,做题可能错
else
cout << "不存在" << endl;
}
return ;
}
输入
6
4 6 2 10 8 12
5
15
输出
地址 0x6d62ac
位置 3
值 6
地址 0x6d62bc
位置 7
不存在
结构体的不会写。。。。。
2、upper_bound()
这个的算的是第一个大于 key 的数。。。没了???是的,这就完了,用法跟 lower_bound() 一样。
二分函数 lower_bound()的更多相关文章
- 分治算法(二分查找)、STL函数库的应用第五弹——二分函数
分治算法:二分查找!昨天刚说不写算法了,但是突然想起来没写过分治算法的博客,所以强迫症的我…… STL函数库第五弹——二分函数lower_bound().upper_bound().binary_se ...
- 二分检索函数lower_bound()和upper_bound()
二分检索函数lower_bound()和upper_bound() 一.说明 头文件:<algorithm> 二分检索函数lower_bound()和upper_bound() lower ...
- STL中的二分查找———lower_bound,upper_bound,binary_search
关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
STL中的二分查找函数 1.lower_bound函数 在一个非递减序列的前闭后开区间[first,last)中.进行二分查找查找某一元素val.函数lower_bound()返回大于或等于val的第 ...
- Long Jumps(二分查找lower_bound()函数的运用)
Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long ju ...
- 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 ...
- 关于函数lower_bound()如何使用的问题
这个函数是c++ STL里自带的函数,应该需要引用头文件#include<iostream> 功能:在一个有序的序列中查找可以将value(一个变量)放在队列里面而不会引起序列长度变化,单 ...
- STL函数 lower_bound 和 upper_bound 在算法竞赛中的用法
以前比较排斥这两个函数,遇到需要二分的情景都是手写 \(while(left<=right)\). 这次决定洗心革面记录一下这两个函数的在算法竞赛中的用法,毕竟一般不会导致TLE. 其实百度百科 ...
- Project Euler 45 Triangular, pentagonal, and hexagonal( 二分 + 函数指针 )
题意: 三角形数.五边形数和六角形数分别由以下公式给出: 三角形数 Tn=n(n+1)/2 1, 3, 6, 10, 15, - 五边形数 Pn=n(3n−1)/2 1, 5, 12, 2 ...
随机推荐
- python gridsearchcv 里的评价准则
http://scikit-learn.org/stable/modules/model_evaluation.html 3.3.1. The scoring parameter: defining ...
- location.reload() 和 location.replace()的区别和应用
首先介绍两个方法的语法: reload 方法,该方法强迫浏览器刷新当前页面.语法: location.reload([bForceGet]) 参数: bForceGet, 可选参数, 默认为 fals ...
- redis的一些简介
Redis是Remote Dictionary Server的缩写,他本质上一个Key/Value数据库,与Memcached类似的NoSQL型数据库. 1. redis的数据类型: st ...
- 编写高质量代码改善C#程序的157个建议——建议61:避免在finally内撰写无效代码
建议61:避免在finally内撰写无效代码 在阐述建议之前,需要先提出一个问题:是否存在一种打破try-finally执行顺序的情况,答案是:不存在(除非应用程序本身因为某些很少出现的特殊情况在tr ...
- ORCHARD学习教程-安装
安装说明:测试对象为正式版1.8 安装方法: 使用Microsoft Web Platform Installer 利用Microsoft WebMatrix 来安装 Working with Orc ...
- Fragment基本介绍
1.生命周期 onCreateView() :创建视图 onActivityCreate():activity创建完成的时候调用 onDestoryView():销毁视图 onDetach():与ac ...
- python之numpy文件操作
目录 numpy 中的文件操作总结 CVS文件 多维数据的存取 numpy 的便捷文件存取 numpy 中的文件操作总结 CVS文件 CSV (Comma‐Separated Value,逗号分隔值) ...
- mysql sp 练习游标和预编译
create procedure Jack_count_cur_dual() BEGIN ); ; DECLARE mycur CURSOR for SELECT table_name FROM tt ...
- 数值限制------c++程序设计原理与实践(进阶篇)
每种c++的实现都在<limits>.<climits>.<limits.h>和<float.h>中指明了内置类型的属性,因此程序员可以利用这些属性来检 ...
- Delphi XE8中开发DataSnap程序常见问题和解决方法 (二)想对DBExpress的TSQLDataSet写对数据库操作的SQL语句出错了!
当我们搞定DataSnap后,我们进入客户端程序开发阶段了,我们建立了客户端模块后,打算按照刚才开发服务器的步骤开发客户端程序,随后加入了DBExpress的TSQLDataSet,设定数据库连接后, ...