这篇博客说是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. integer encoding vs 1-hot (py)

    https://github.com/szilard/benchm-ml/issues/1 glouppe commented on 7 May 2015 Thanks for the benchma ...

  2. [原创]MongoDB C++ 驱动部分问题解决方案(MongoDB C++ Driver)

    本文为我长时间开发以及修改MongoDB C++ Driver时的一些问题和解决方案.目前本文所介绍的相关引擎也已经发布闭源版本,请自行下载 库版本以及相关位置:http://code.google. ...

  3. js 二维数组排序sort()函数

    一.按数值排序 var arr = [[1, 2, 3], [7, 2, 3], [3, 2, 3]]; arr.sort(function(x, y){  return x[0] – y[0];}) ...

  4. 面试题:测试给定的list,使用for,foreach,iterator删除元素的不同表现

    上代码: 1. 使用增强for循环(foreach) package com.xxx; import java.util.ArrayList; import java.util.List; /** * ...

  5. Network in Network(NiN)

    - Mlpconv layer with "micronetwork" with each conv layer to compute more abstract features ...

  6. DELPHI XE5 UP2 运行IOS 遇到 Wrapper init failed: (null)问题的解决办法

    一.问题表现: 在MAC OSX(10.9.2)上安装了比较新的XCODE5.1 和COMMAND LINE TOOLS 在DELPHI XE5 UP2上放了一个按钮,输出到MAC OSX上,出现: ...

  7. java 支付宝即时到帐提交订单dome

    package com.tian.batis; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; imp ...

  8. asp.net winform 界面传值

    第一种 //form1 //静态传值 public static string Chuanzhi; string Chuanzhi = textbox.text; //form2 string Chu ...

  9. Linux中,关闭selinux

    首先我们可以用命令来查看selinux的状态getenforce 这个命令可以查看到selinux的状态,当前可以看到是关闭状态的. 还有一个命令也可以查看出selinux的状态.sestatus - ...

  10. eclipse 开发hbase 环境准备

    修改开发机的host文件: