二分函数 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 ...
随机推荐
- hdu 4740 The Donkey of Gui Zhou
1.扯犊子超多if else 判断的代码,华丽丽的TLE. #include<stdio.h> #include<string.h> #define N 1010 int ma ...
- Requests接口测试(四)
Python序列化和反序列化 啥是序列化?啥是反序列化?这两个词听起来优点高大上的意思,其实呢不然,很简单的可以理解为: 序列化:将python的数据对象编码转换为json格式的字符串 反序列化:将j ...
- Azure 执行模型
最后更新时间(英文版):01/20/2015 最后更新时间(中文版):04/11/2015 Azure 提供了用于运行应用程序的不同执行模型.每种模型提供一组不同服务,而你选择哪种模型完全取决于你要做 ...
- Java 数据结构之数组
public class Arrays { //创建一个Integer空数组 public static Integer[] player=null; //添加球员号码 pri ...
- 解决Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of the configured nodes are available
Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of ...
- git vs sourcetree
SourceTree&Git部分名词解释 克隆(clone):从远程仓库URL加载创建一个与远程仓库一样的本地仓库 提交(commit):将暂存文件上传到本地仓库(我们在Finder中对本地仓 ...
- C# 给图片添加透明的文字、图片水印
#region 添加水印 /// <summary> /// 添加文字水印 /// </summary> /// <param name="image" ...
- OO 抽象类与接口的区别
抽象类与接口的区别 抽象类与接口的区别 一.抽象类:(抽象类适用于同一系列,并且有需要继承的成员) 概念: 1.使用abstract修饰: 2.抽象类中可以包含抽象方法: 3.抽象类只能被子类继承:( ...
- leecode刷题(6)-- 两个数组的交集II
leecode刷题(6)-- 两个数组的交集II 两个数组的交集II 描述: 给定两个数组,编写一个函数来计算它们的交集. 示例: 输入: nums1 = [1,2,2,1], nums2 = [2, ...
- Django 实现购物车功能
购物车思路:使用 session 功能识别不同浏览器用户,使得用户不管是否登录了网站,均能够把想要购买的产品放在某个地方,之后随时可以显示或修改要购买的产品,等确定了之后再下订单,购物车可以用来暂存商 ...