- Use the lower_bound function from the header in C++.
- The lower_bound function takes two parameters: the beginning and end of a sorted range, and a value to search for.
- It returns an iterator pointing to the first element that is not less than the given value.
- If the value is not found, it returns an iterator pointing to the first element that is greater than the given value.
- Here's an example:
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> vec = {10, 20, 30, 40, 50};
auto it = std::lower_bound(vec.begin(), vec.end(), 25);
if (it != vec.end()) {
std::cout << "Found at index: " << std::distance(vec.begin(), it) << std::endl;
} else {
std::cout << "Not found" << std::endl;
}
return 0;
}