#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
// 创建一个有序的整数向量
vector<int> nums = {1, 2, 2, 3, 4, 6, 8};
// 要查找的值
int target = 3;
// 使用 lower_bound 查找第一个不小于 target 的元素
vector<int>::iterator it = lower_bound(nums.begin(), nums.end(), target);
// 检查是否找到了有效的迭代器
if (it != nums.end()) {
cout << "First element not less than " << target << " is " << *it << endl;
} else {
cout << "No element found that is not less than " << target << endl;
}
return 0;
}
lower_bound
是 C++ 标准库中的一个算法,用于在已排序的范围内查找第一个不小于给定值的元素。nums
,并使用 lower_bound
查找第一个不小于目标值 target
的元素。lower_bound
返回一个指向该元素的迭代器。如果找到这样的元素,则输出该元素的值;否则,输出提示信息表示没有找到符合条件的元素。上一篇:c++pair
下一篇:c++ to_string
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站