这篇博客说是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()的更多相关文章

  1. 分治算法(二分查找)、STL函数库的应用第五弹——二分函数

    分治算法:二分查找!昨天刚说不写算法了,但是突然想起来没写过分治算法的博客,所以强迫症的我…… STL函数库第五弹——二分函数lower_bound().upper_bound().binary_se ...

  2. 二分检索函数lower_bound()和upper_bound()

    二分检索函数lower_bound()和upper_bound() 一.说明 头文件:<algorithm> 二分检索函数lower_bound()和upper_bound() lower ...

  3. STL中的二分查找———lower_bound,upper_bound,binary_search

    关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...

  4. STL中的二分查找——lower_bound 、upper_bound 、binary_search

    STL中的二分查找函数 1.lower_bound函数 在一个非递减序列的前闭后开区间[first,last)中.进行二分查找查找某一元素val.函数lower_bound()返回大于或等于val的第 ...

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

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

  7. 关于函数lower_bound()如何使用的问题

    这个函数是c++ STL里自带的函数,应该需要引用头文件#include<iostream> 功能:在一个有序的序列中查找可以将value(一个变量)放在队列里面而不会引起序列长度变化,单 ...

  8. STL函数 lower_bound 和 upper_bound 在算法竞赛中的用法

    以前比较排斥这两个函数,遇到需要二分的情景都是手写 \(while(left<=right)\). 这次决定洗心革面记录一下这两个函数的在算法竞赛中的用法,毕竟一般不会导致TLE. 其实百度百科 ...

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

随机推荐

  1. 264E Roadside Trees

    传送门 题目大意 分析 倒着跑LIS表示以i为开头的LIS,于是对于删除可以暴力重算前10棵树.而对于种树,因为高度不超过10且高度两两不同,所以暴力重算比它矮的10棵树即可.对于需要重算的点,将其从 ...

  2. 无法链接到windows服务

    1.先将鼠标移动到桌面右下角的显示桌面按钮处,选择右侧按钮列表中的搜索列表,输入cmd.exe,右击搜索结果,选择以管理员身份运行. 2.然后输入netsh winsock reset catalog ...

  3. Java静态变量的用法:伪单例

    这几天遇到一个问题,一个Service里有一个map,但是这个Service有别的继承,于是每一个Service都会创建一个map,但是这个map应该是公用的,于是就有问题了...(按结构说Servi ...

  4. C#上位机中ZedGraph控件的使用

    上位机程序控制PLC模拟量通道输出周期性正弦波信号,并采集所造成改变的模拟量输入信号,并绘制数据变化曲线. 界面如图: 最后测试效果如图: 代码: using System; using System ...

  5. 「BZOJ1000」A+B Problem

    写这个主要是为了凑\(BZOJ\)题解用的,不用在意.跳过即可 \(Code\) #include<bits/stdc++.h> using namespace std; int main ...

  6. 使用C/C++代码编写Python模块

    假如我们要用C语言实现下面的python脚本bird.py import os def fly(name): print(name + " is flying.\n") 调用脚本m ...

  7. time,datetime,calendar模块

    Python中,与时间有关的模块有time,datetime和calendar. 1.时钟时间:time 在Python中,用三种方式来表示时间:时间戳,格式化时间字符串和结构化时间. 1)时间戳,就 ...

  8. 【大数据系统架构师】1.2 大数据基础Hadoop 2.X

    1. hadoop环境搭建 1.1 伪分布式环境搭建 1.1.1 伪分布式环境搭建 1.1.2 伪分布式搭建结果 hdfs可视化界面: http://od001:50070/dfshealth.htm ...

  9. linux下vim python代码自动补全

    一.vim python自动补全插件:pydiction 可以实现下面python代码的自动补全: 1.简单python关键词补全 2.python 函数补全带括号 3.python 模块补全 4.p ...

  10. VS2010设置默认浏览器

    1.选择项目中起始页右键,然后选择浏览方式 2.然后就可以设置默认浏览器了