首先,先定义数组

int a[10];

这是今天的主角.

这四个函数都是在数组上操作的

注意要包含头文件

#include<algorithm>

sort:

sort(a,a+10)

对十个元素进行排序,顺序为从小到大.

sort(a,a+10);
    for(int i=0;i<10;i++)
    {
        printf("%d ",a[i]);
    }

自定义sort

sort (a,a+10,Rule());

按照Rule给定的规则进行排序.

Rule函数的写法:(以比较个位数字为例)

    struct Rule
{
bool operator()(const int &a,const int &b)
{
return a%>b%;
}
};

注意,这个要写在开头,main外面

binary_search

首先需要注意,是下划线,不要写错.

binary_search(a,a+10,7,排序规则)//以查找数字7为例

可以没有排序规则,也就是

binary_search(a,a+10,7)//查找数字7

但是,在查找之前,一定要注意要先排好序.也就是用一下sort.

sort  binary_search的排序规则应保持一致

还有就是binary_search 返回的是 个数.

没有查找到的返回零,

所以可以用来做函数的返回值.

代码:

查找615的个数

     int b=    binary_search(a,a+,);
printf("%d",b);
printf("\n");
int bb= binary_search(a,a+,,Rule());
printf("%d",bb);
printf("\n");

lower_bound查找区间下标最小的且大于等于值的元素的地址

upper_bound查找区间下标最小的且大于值的元素的地址

    int *p1,*p2;
p1=lower_bound(a,a+,);
printf("%d %d\n",a[p1-a],p1-a);
p2=upper_bound(a,a+,);
printf("%d %d\n",a[p2-a],p2-a);

注意:

p1-a是指其所在的下标

问题:

注意,只要用了sort,数组里的元素顺序就会改变,而查找必须是在对应的排序下的.所以在进行完一次查找时,需要进行下一次查找之前,要在进行一次需要规则的排序.

记住,它顺序在改变!!

STL入门--sort,lower_bound,upper_bound,binary_search及常见错误的更多相关文章

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

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

  2. STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())

    一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search  -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...

  3. stl lower_bound upper_bound binary_search equal_range

    自己按照stl实现了一个:   http://www.cplusplus.com/reference/algorithm/binary_search/ 这里有个注释,如何判断两个元素相同: Two e ...

  4. STL中的unique()和lower_bound ,upper_bound

    unique(): 作用:unique()的作用是去掉容器中相邻元素的重复元素(数组可以是无序的,比如数组可以不是按从小到大或者从大到小的排列方式) 使用方法:unique(初始地址,末地址): 这里 ...

  5. STL algorithm算法lower_bound和upper_bound(31)

    lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...

  6. [STL]lower_bound&upper_bound

    源码 lower_bound template <class ForwardIterator, class T> ForwardIterator lower_bound (ForwardI ...

  7. lower_bound && upper_bound

     用lower_bound进行二分查找 ●在从小到大排好序的基本类型数组上进行二分查找. 这是二分查找的一种版本,试图在已排序的[first,last)中寻找元素value.如果[first,last ...

  8. C++ lower_bound/upper_bound用法解析

    1. 作用           lower_bound和upper_bound都是C++的STL库中的函数,作用差不多,lower_bound所返回的是第一个大于或等于目标元素的元素地址,而upper ...

  9. lower_bound/upper_bound example

    http://www.cplusplus.com/reference/algorithm/upper_bound/左闭右开 Return iterator to lower bound Returns ...

随机推荐

  1. 深入理解JVM(③)Java模块化系统

    前言 JDK9引入的Java模块化系统(Java Platform Module System ,JPMS)是 对Java技术的一次重要升级,除了像之前JAR包哪有充当代码的容器之外,还包括: 依赖其 ...

  2. Typography convention

    1 h1 Chapter title centered,number three in bold,used ##. 1.1 h2 The chapter is a section, and the s ...

  3. jQurey zTree Demo 3.5

    https://jeesite.gitee.io/front/jquery-ztree/3.5/demo/cn/index.html

  4. centos7时间调整

    查看时区是否正确,命令date -R: 不正确先调整时区,命令tzselect: 安装ntp,命令yum install ntp: 同步时间,命令ntpdate cn.pool.ntp.org: 设置 ...

  5. Oracle Solaris 10下gdb安装(附安装包)

    文章目录 1. 背景说明 2. gdb相关包 3. gdb安装 3.1 上传资源 3.2 解压 3.3 安装 3.4 环境变量 4. 位数确认 5. 验证可用性 1. 背景说明 本文承接Oracle ...

  6. springboot集成springDataJpa

    1.引用依赖 <!--spring-data-jpa--> <dependency> <groupId>org.springframework.boot</g ...

  7. Electron 初识-搭建一个简易桌面应用

    Electron ​ 快速入门 简介 Electron 可以让你使用纯 JavaScript 调用丰富的原生 APIs 来创造桌面应用.你可以把它看作是专注于桌面应用而不是 web 服务器的,io.j ...

  8. java语言进阶(一)_Object类_常用API

    第一章 Object类 1.1 概述 java.lang.Object类是Java语言中的根类,即所有类的父类.它中描述的所有方法子类都可以使用.在对象实例化的时候,最终找的父类就是Object. 如 ...

  9. c# 操作Redis的五种基本类型总结

    在我们的项目中,通常会把数据存储到关系型数据库中,比如Oracle,SQL Server,Mysql等,但是关系型数据库对于并发的支持并不是很强大,这样就会造成系统的性能不佳,而且存储的数据多为结构化 ...

  10. web前端知识点(JavaScript篇)

    call,apply,bind call,apply,bind这三者的区别,及内部实现原理,点这里 promise promise函数的内部实现原理,点这里 闭包 闭包就是能够读取其他函数内部变量的函 ...