




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
c++二分查找库函数二分查找也叫折半查找,是一种在有序数组中快速定位目标值的常用算法。C++标准库提供了二分查找的库函数`binary_search`和`lower_bound`、`upper_bound`、`equal_range`三个相关函数。
1.`binary_search`函数:
`binary_search(first,last,value)`函数用来判断有序范围`[first,last)`是否包含了目标值`value`,返回一个bool值。它首先会用二分查找算法找到`value`可能出现的位置,然后返回`true`或者`false`。
```cpp
#include<algorithm>
#include<iostream>
#include<vector>
intmain(){
std::vector<int>nums{1,2,3,4,5,6,7,8,9};
intvalue=6;
if(std::binary_search(nums.begin(),nums.end(),value)){
std::cout<<"Found"<<value<<"inthevector."<<std::endl;
}else{
std::cout<<"Cannotfind"<<value<<"inthevector."<<std::endl;
}
return0;
}
```
2.`lower_bound`函数:
`lower_bound(first,last,value)`函数用来在有序范围`[first,last)`中寻找第一个大于或等于目标值`value`的位置,返回一个迭代器。如果没有找到满足条件的元素,返回`last`。可以通过迭代器的差值来得到目标值在容器中的索引。
```cpp
#include<algorithm>
#include<iostream>
#include<vector>
intmain(){
std::vector<int>nums{1,2,3,4,5,6,7,8,9};
intvalue=6;
autoit=std::lower_bound(nums.begin(),nums.end(),value);
if(it!=nums.end()){
intindex=std::distance(nums.begin(),it);
std::cout<<"Found"<<value<<"atindex"<<index<<"."<<std::endl;
}else{
std::cout<<"Cannotfind"<<value<<"inthevector."<<std::endl;
}
return0;
}
```
3.`upper_bound`函数:
`upper_bound(first,last,value)`函数用来在有序范围`[first,last)`中寻找第一个大于目标值`value`的位置,返回一个迭代器。如果没有找到满足条件的元素,返回`last`。可以通过迭代器的差值来得到目标值在容器中的索引。
```cpp
#include<algorithm>
#include<iostream>
#include<vector>
intmain(){
std::vector<int>nums{1,2,3,4,5,6,7,8,9};
intvalue=6;
autoit=std::upper_bound(nums.begin(),nums.end(),value);
if(it!=nums.end()){
intindex=std::distance(nums.begin(),it);
std::cout<<"Found"<<value<<"atindex"<<index<<"."<<std::endl;
}else{
std::cout<<"Cannotfind"<<value<<"inthevector."<<std::endl;
}
return0;
}
```
4.`equal_range`函数:
`equal_range(first,last,value)`函数用来在有序范围`[first,last)`中查找与目标值`value`相等的元素,并返回一个包含两个迭代器的pair对象,分别表示第一个匹配的元素和最后一个匹配的元素的后一个位置。如果没有找到满足条件的元素,两个迭代器都指向目标值应该插入的位置。
```cpp
#include<algorithm>
#include<iostream>
#include<vector>
intmain(){
std::vector<int>nums{1,2,3,4,5,6,6,6,7,8,9};
intvalue=6;
std::pair<std::vector<int>::iterator,std::vector<int>::iterator>range;
range=std::equal_range(nums.begin(),nums.end(),value);
if(range.first!=range.second){
intstart=std::distance(nums.begin(),range.first);
intend=std::distance(nums.begin(),range.second);
std::cout<<"Found"<<value<<"betweenindex"<<start<<"and"<<end-1<<"."<<std::endl;
}else{
std::cout<<"Can
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 湖南都市职业学院《现代建筑企业运营管理》2023-2024学年第二学期期末试卷
- 盐城工学院《免疫学原理及技术》2023-2024学年第二学期期末试卷
- 浙江东方职业技术学院《影视后期特效设计》2023-2024学年第二学期期末试卷
- 洛阳科技职业学院《建筑工业化与装配式建筑》2023-2024学年第二学期期末试卷
- 湖南汽车工程职业学院《中国当代文学(二)》2023-2024学年第二学期期末试卷
- 武汉设计工程学院《生理学》2023-2024学年第二学期期末试卷
- 陕西邮电职业技术学院《都市型现代农业概论》2023-2024学年第二学期期末试卷
- 山西大同大学《仪器分析(光谱)》2023-2024学年第二学期期末试卷
- 福建华南女子职业学院《案例分析》2023-2024学年第二学期期末试卷
- 贵州民族大学《工程训练(Ⅱ)B》2023-2024学年第二学期期末试卷
- (部编版)统编版小学语文教材目录(一至六年级上册下册齐全)
- 食管纵隔瘘的护理
- 县医院聘请社会监督员实施方案(经典版)
- 二《风景谈》公开课一等奖创新教学设计中职语文高教版(2023-2024)基础模块上册
- 人教版初中道德与法治九年级下册全册教学课件
- 教学工作奖励办法
- 会员卡转让协议书范本(2024版)
- 育婴师培训课件
- 2024年扬州市职业大学单招职业适应性测试题库附答案
- 中药材种植中药材种植良种繁育技术研究与应用
- 安徽省皖江名校联盟2024届高三下学期4月二模化学
评论
0/150
提交评论