vector lowerbound
时间: 2023-08-21 19:13:11 浏览: 52
`lower_bound` 是一个 C++ 标准库算法,用于在已排序的容器中查找第一个大于或等于给定值的元素,并返回一个迭代器指向该元素。
对于 `vector` 容器,你可以使用 `lower_bound` 函数来获取指定值的下界。下面是一个示例代码:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> v = {1, 3, 5, 7, 9};
int target = 4;
auto it = std::lower_bound(v.begin(), v.end(), target);
if (it != v.end()) {
std::cout << "The lower bound for " << target << " is " << *it << std::endl;
} else {
std::cout << "No lower bound found for " << target << std::endl;
}
return 0;
}
```
在上面的示例中,我们创建了一个整数类型的 `vector` 容器,并使用 `lower_bound` 函数查找目标值为 4 的元素的下界。输出结果将为 "The lower bound for 4 is 5",因为在容器中,第一个大于或等于 4 的元素是 5。
希望这个示例能够帮助你理解 `lower_bound` 在 `vector` 中的用法。如果你有任何其他问题,请随时提问!