二分函数 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 ...
随机推荐
- Django框架 之 Auth用户认证
Django框架 之 Auth用户认证 浏览目录 auth模块 user对象 一.auth模块 1 from django.contrib import auth django.contrib.aut ...
- hdu 2553 N皇后问题(一维数组详尽解释)
//一维数组解法(注释详尽)//num皇后可以表示第num列,然后枚举num皇后所在的行//二维数组对角线转换为坐标的关系#include<stdio.h> #include<str ...
- 何为软件的Alpha、Beta、RC和GA发布版本?
简介 一个软件或者一个功能在发布时,通常会有Beta版这么一说.我很熟悉,差不多知道是什么意思,但没去深究,感觉上就是一个可以用但不保证功能稳定的版本. 直到昨天我看到了 MariaDB 数据库发布标 ...
- 如何解决某个jar包的依赖冲突问题
我用的是idea集成开发环境,因此以该开发工具讲解. 首先在在Terminal窗口中,键入:mvn dependency:tree -Dincludes=com.google.guava 如果不加-D ...
- C# 接口(3)
这么半天说了如何使用,实现接口.相信也都发现了接口和抽象类很多相似的地方. 但是! 这两个根本就是不一样的. 抽象类 : ...
- 「BZOJ 1876」「SDOI 2009」SuperGCD「数论」
题意 求\(\gcd(a, b)\),其中\(a,b\leq10^{10000}\) 题解 使用\(\text{Stein}\)算法,其原理是不断筛除因子\(2\)然后使用更相减损法 如果不筛\(2\ ...
- 630. Course Schedule III
There are n different online courses numbered from 1 to n. Each course has some duration(course leng ...
- wx-xcx
一.因测试小程序,搜索了各种测试小程序的注意点,为方便后续查看,暂时整理罗列如下: 1.留意分享功能的权限:如果某页面对游客访问有权限限制,则需要设置取消其分享功能. 2.小程序强制更新功能:新版小程 ...
- linux 常用端口【转载】
原文地址: http://blog.csdn.net/u013943420/article/details/65938696 一个计算机最多有65535个端口,端口不能重复.这65536个端口被分为两 ...
- numpy常用功能总结、python格式化输入输出
#coding:utf-8 #author:徐卜灵 ##################### #由于在各大公司笔试的时候总是会遇到一些格式化输入输出数据,今天就来总结一下. #结合numpy来处理数 ...