、std::lower_bound
时间: 2024-06-16 09:06:06 浏览: 9
std::lower_bound是C++标准库中的一个函数,它用于在有序序列中查找第一个大于或等于给定值的元素的位置。它的函数原型如下:
```cpp
template <class ForwardIterator, class T>
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val);
```
其中,`first`和`last`是表示序列范围的迭代器,`val`是要查找的值。lower_bound函数返回一个迭代器,指向序列中第一个大于或等于`val`的元素。
lower_bound函数使用二分查找算法来进行查找,因此要求序列必须是有序的。如果序列中存在与`val`相等的元素,则返回该元素的位置;如果序列中不存在大于或等于`val`的元素,则返回最后一个元素的下一个位置。
lower_bound函数在实际应用中非常有用,可以用于在有序数组或容器中快速查找某个值的位置,或者确定插入某个值时的正确位置。
相关问题
std::lower_bound
std::lower_bound是C++ STL中的一个函数模板,用于在已排序的区间内查找某个特定值的位置。它返回一个迭代器,指向区间中第一个不小于给定值的元素。std::lower_bound有多个版本,具体使用哪个版本取决于参数的类型和数量。
版本一是最基本的版本,接受三个参数:一个表示区间起始位置的迭代器first,一个表示区间末尾位置的迭代器last,以及要查找的值value。这个版本使用默认的比较函数operator<来进行比较。它返回的迭代器指向第一个不小于value的元素,如果所有元素都小于value,则返回last。
版本二是C++20引入的新版本,接受三个参数:一个表示区间起始位置的迭代器first,一个表示区间末尾位置的迭代器last,以及要查找的值value。这个版本使用默认的比较函数operator<来进行比较。它返回的迭代器指向第一个不小于value的元素,如果所有元素都小于value,则返回last。
版本三也是C++20引入的新版本,接受四个参数:一个表示区间起始位置的迭代器first,一个表示区间末尾位置的迭代器last,要查找的值value,以及一个比较函数对象comp。这个版本使用自定义的比较函数comp来进行比较。它返回的迭代器指向第一个不小于value的元素,如果所有元素都小于value,则返回last。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [C++11标准模板(STL)- 算法 - 二分搜索操作(在已排序范围上)(std::lower_bound)](https://blog.csdn.net/qq_40788199/article/details/127990308)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
、std::lower_bound的使用
std::lower_bound是C++标准库中的一个函数,用于在有序序列中查找第一个大于或等于给定值的元素的位置。它的使用方式如下:
```cpp
template <class ForwardIterator, class T>
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val);
```
其中,`first`和`last`是表示序列范围的迭代器,`val`是要查找的值。函数返回一个迭代器,指向序列中第一个大于或等于`val`的元素。
下面是一个示例代码,演示了如何使用std::lower_bound:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int target = 6;
auto it = std::lower_bound(nums.begin(), nums.end(), target);
if (it != nums.end()) {
std::cout << "找到了大于或等于" << target << "的元素:" << *it << std::endl;
} else {
std::cout << "没有找到大于或等于" << target << "的元素" << std::endl;
}
return 0;
}
```
输出结果为:
```
找到了大于或等于6的元素:6
```
如果要查找的值在序列中不存在,std::lower_bound会返回一个指向序列中第一个大于该值的元素的迭代器,或者返回`last`迭代器(表示查找失败)。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)