lower_bound;
时间: 2023-10-14 19:05:12 浏览: 87
lower_bound是一个在已排序的数组或容器中查找第一个大于等于给定值的元素位置的算法。它的原型实现是使用二分查找的方式来进行查找。具体实现如下:
```cpp
int lower_bound(vector<int>& nums, int x) {
int left = 0;
int right = nums.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (x > nums[mid]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left;
}
```
在使用lower_bound时,可以参考中的示例应用。它用于查找一个有序数组中第一个大于等于给定值的元素位置。更详细的用法可以参考中的C++折半查找的相关介绍。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [【C++】从没见过这么详细的lower_bound的讲解](https://blog.csdn.net/weixin_43939593/article/details/105602530)[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: 50%"]
- *3* [C++ 折半查找之 lower_bound 和 upper_bound](https://blog.csdn.net/qq_42303573/article/details/128082762)[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: 50%"]
[ .reference_list ]
阅读全文