lower bound c++

  1. Use the lower_bound function from the header in C++.
  2. The lower_bound function takes two parameters: the beginning and end of a sorted range, and a value to search for.
  3. It returns an iterator pointing to the first element that is not less than the given value.
  4. If the value is not found, it returns an iterator pointing to the first element that is greater than the given value.
  5. 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;
}