有关lower_bound()函数的使用
来源:互联网 发布:网络教育录取时间 编辑:程序博客网 时间:2024/05/22 06:49
lower_bound()函数需要加载头文件#include<algorithm>,其基本用途是查找有序区间中第一个大于或等于某给定值的元素的位置,其中排序规则可以通过二元关系来表示。
函数原型:
template<class ForwardIterator, class Type> ForwardIterator lower_bound( ForwardIterator _First, ForwardIterator _Last, const Type& _Val );template<class ForwardIterator, class Type, class BinaryPredicate> ForwardIterator lower_bound( ForwardIterator _First, ForwardIterator _Last, const Type& _Val, BinaryPredicate _Comp );
传入参数说明:
_First 要查找区间的起始位置
_Last 要查找区间的结束位置
_Val 给定用来查找的值
_Comp 自定义的表示小于关系的函数对象,根据某个元素是否满足小于关系而返回true或者false举例说明:
#include<iostream>#include<vector>#include<algorithm>using namespace std;vector<int> v;int main(){ for (int i = 1; i < 4; i++) v.push_back(2 * i);//注意此时v中的元素本身就是有序的 vector<int>::iterator it = lower_bound(v.begin(), v.end(), 3); cout << *it << endl; return 0;}上面的例子是针对容器的,注意返回的是距离元素3最近的指针it,输出的是*it结果为元素4,假如我想得到位置而非具体的元素应该怎么办呢?这里有一个指针偏移的技巧,只需要减去起始位置的指针即可,代码如下:
#include<iostream>#include<vector>#include<algorithm>using namespace std;vector<int> v;int main(){ for (int i = 1; i < 4; i++) v.push_back(2 * i);//注意此时v中的元素本身就是有序的 //vector<int>::iterator it = lower_bound(v.begin(), v.end(), 3); int pos = lower_bound(v.begin(), v.end(), 3)-v.begin(); cout << pos<< endl; return 0;}这时候返回pos就是所查找元素的位置,下标,这里查找到的元素应该是4在容器中的下标是1,所以输出pos的结果就是1.对容器适用,对数组同样适用:#include<iostream>#include<algorithm>using namespace std;int main(){ int a[4] = { 2, 4, 6, 8 }; //注意此时a中的元素本身就是有序的 int * it = lower_bound(a,a+4,3); cout << *it<< endl; return 0;}返回位置只需要减掉数组的起始位置即可:#include<iostream>#include<algorithm>using namespace std;int main(){ int a[4] = { 2, 4, 6, 8 }; //注意此时a中的元素本身就是有序的 //int * it = lower_bound(a,a+4,3); int pos = lower_bound(a, a + 4, 3) - a;//a表示数组的起始位置 cout <<pos<< endl; return 0;}结果和容器的时候是一样的。
对于4个参数的情形,最后一个参数的自己定义的表示大小关系函数的对象,常用的逆序可以加载头文件#include<functional>,里边有一个greater<int>()函数即可对逆序求最近位置。假如说像上边一样元素为2 4 6 8,逆序则是8 6 4 2,那么求距离3最近表示的是与3最近的小于等于3的元素,输出结果则是元素2了,代码如下:
#include<iostream>#include<vector>#include<algorithm>#include<functional>using namespace std;vector<int> v;int main() { for (int i = 4; i >0; i--) v.push_back(2 * i);//注意此时v中的元素本身就是有序的 vector<int>::iterator it = lower_bound(v.begin(), v.end(), 3,greater<int>()); cout << *it << endl; return 0;}说明,要查找的有序序列必须是合法的,已经被排序的序列。
0 0
- 有关lower_bound()函数的使用
- 有关lower_bound的比较函数
- lower_bound函数的使用
- 函数lower_bound()的使用
- lower_bound()与upper_bound()函数的使用
- lower_bound upper_bound 【 二分函数的使用】
- lower_bound() 的使用
- lower_bound的使用
- STL--lower_bound()函数的用法
- map::lower_bound/upper_bound的使用
- map::lower_bound/upper_bound的使用
- upper_bound()和lower_bound()的使用
- lower_bound函数
- lower_bound函数
- lower_bound函数
- lower_bound函数
- 论c++的二分函数lower_bound
- lower_bound函数的用法(STL库函数)
- 期待读全书——像外行一样思考,像专家一样实践
- ZOJ3862:Intersection
- 几种开放源码的TCPIP协议栈概述--LwIP,uIP,TinyTcp和uC/IP
- vijos 1404 遭遇战[spfa]
- SCALA 安装和 SACLA ECLIPSE PLUGIN安装
- 有关lower_bound()函数的使用
- HDU 2897 邂逅明下 (找规律)
- Android Fresco图片处理库用法API英文原文文档1(Facebook开源Android图片库)
- 使用sugar crm rest api 创建联系人时有概率返回被删除的联系人的id
- 深入理解java异常处理机制
- SDOI2015游记
- 早就注册了CSDN
- Swiper说明&&API手册 【中文手册Swiper】
- 算法导论学习之--二叉搜索树