STL入门--sort,lower_bound,upper_bound,binary_search及常见错误
首先,先定义数组
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及常见错误的更多相关文章
- STL中的二分查找———lower_bound,upper_bound,binary_search
关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...
- STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())
一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...
- stl lower_bound upper_bound binary_search equal_range
自己按照stl实现了一个: http://www.cplusplus.com/reference/algorithm/binary_search/ 这里有个注释,如何判断两个元素相同: Two e ...
- STL中的unique()和lower_bound ,upper_bound
unique(): 作用:unique()的作用是去掉容器中相邻元素的重复元素(数组可以是无序的,比如数组可以不是按从小到大或者从大到小的排列方式) 使用方法:unique(初始地址,末地址): 这里 ...
- STL algorithm算法lower_bound和upper_bound(31)
lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...
- [STL]lower_bound&upper_bound
源码 lower_bound template <class ForwardIterator, class T> ForwardIterator lower_bound (ForwardI ...
- lower_bound && upper_bound
用lower_bound进行二分查找 ●在从小到大排好序的基本类型数组上进行二分查找. 这是二分查找的一种版本,试图在已排序的[first,last)中寻找元素value.如果[first,last ...
- C++ lower_bound/upper_bound用法解析
1. 作用 lower_bound和upper_bound都是C++的STL库中的函数,作用差不多,lower_bound所返回的是第一个大于或等于目标元素的元素地址,而upper ...
- lower_bound/upper_bound example
http://www.cplusplus.com/reference/algorithm/upper_bound/左闭右开 Return iterator to lower bound Returns ...
随机推荐
- igate(因特网网关)
网关:Gateway 又称网间连接器.协议转换器.-----复杂的网络互连设备. 网关在网络层以上实现网络互连,是复杂的网络互连设备,仅用于两个高层协议不同的网络互连.网关既可以用于广域网互连,也可以 ...
- mac安装Hadoop,mysql,hive,sqoop教程
在安装Hadoop,mysql,hive之前,首先要保证电脑上安装了jdk 一.配置jdk 1. 下载jdk http://www.oracle.com/technetwork/java/javase ...
- 状压DP之炮兵阵地
题目 原题来自:\(NOI 2001\) 司令部的将军们打算在\(N*M\) 的网格地图上部署他们的炮兵部队.一个\(N*M\)的地图由\(N\)行\(M\)列组成,地图的每一格可能是山地(用 H表示 ...
- sql-exists、not exists的用法
exists : 强调的是是否返回结果集,不要求知道返回什么, 比如:select name from student where sex = 'm' and mark exists(select 1 ...
- 一口气说出 OAuth2.0 的四种鉴权方式,面试官会高看一眼
本文收录在个人博客:www.chengxy-nds.top,技术资源共享,一起进步 上周我的自研开源项目开始破土动工了,<开源项目迈出第一步,10 选 1?页面模板成了第一个绊脚石 > , ...
- element-ui 表单校验 Rules 配置 常用黑科技
type 指示type要使用的验证器.可识别的类型值为: string:类型必须为string.type 默认是 string // 校验 string: [ {type: 'string', mes ...
- scrapy(一):基础用法
Scrapy 框架 Scrapy 简介 Scray 是用python写的为了爬取网站数据,提取结构性数据的应用框架 Scrapy框架原理图 白话讲解Scrapy 运作流程 代码写好,程序开始运行... ...
- OSCP Learning Notes - Capstone(1)
Kioptrix Level 1.1 Walkthrough Preparation: Download the virtual machine from the following website ...
- CUDA Programming Guide 学习笔记
CUDA学习笔记 GPU架构 GPU围绕流式多处理器(SM)的可扩展阵列搭建,每个GPU有多个SM,每个SM支持数百个线程并发执行.目前Nvidia推出了6种GPU架构(按时间顺序,详见下图):Fer ...
- echarts 实战 : 标题的富文本样式
官方文档在这一块交待的不是很清楚,记录一下. title:{ left:15, top:10, subtext:"AAA {yellow|316} BBB {blue|219}", ...