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 ...
随机推荐
- $.post 参数定义
//重置密码 function ResetPassword(id, accounts) { //alert("重置密码id-" + id + "-" + acc ...
- 二.2vueadmin-template反向代理/路由配置,idc增查删
一.反向代理: (1)F:\devops\data\web\vueAdmin-template\config\index.js ---让别人也能访问我的vue前端 host: '0.0.0.0', ( ...
- js的几个小问题
1.存一个有效期为7天的cookie,key = nickname, val = Ace 代码: function setCookie(key,val,expires){ let now=new Da ...
- Git 新建版本库命令
Command line instructions Git global setup git config --global user.name "张三" git config - ...
- Windows常用注册表文件
内容转载自我的博客 目录 1. 删除Visual Studio的右键菜单 2. 恢复Visual Studio的右键菜单 3. 右键菜单添加功能 4. USB3.0连接安卓手机刷机出现问题 1. 删除 ...
- 又一款开源图标库 CSS.GG,值得一用
嗨,我是 Martin,也叫老王,今天推荐一款好用的开源图标库. 我们平常找图标往往会去 iconfont 但是今天,我们看了 Martin 的文章之后,就会有一个新的选择--CSS.GG Githu ...
- 利用CSS变量实现炫酷的悬浮效果
最近,我从 Grover网站 上发现以一个好玩儿的悬停动画,这个动画是将鼠标移动到订阅按钮上移动光标,会跟随光标实现相应的彩色渐变. 这个想法很简单,但是它能使这个按钮脱颖而出,人们一下子就注意到它了 ...
- Web前端MVC框架的意义分析
前言: Web前端开发是Web技术发展中的一个重要组成部分,在传统的前端开发中由于外界因素的影响导致其开发形式呈现出简单化的特点,即以页面为主体来展示界面中的信息.然而随着科学技术的不断进步,Web前 ...
- css3中样式计算属性calc()的使用和总结
calc的介绍 在css3样式中有一个类似与函数的计算属性calc(),它主要用于指定元素的长度,无论是border.margin.pading.font-size和width等属性都可以使用calc ...
- JS学习研究
//1.undefined 是派生自null的 //alert(undefined == null); //alert(undefined === null); ////结果 true false / ...