C++ binary search with lower_bound

For ascending sorted input, lower_bound returns the first position whose value is not less than the target, or end if none exists.

C++
#include <algorithm>
#include <iostream>
#include <vector>
#include <cassert>
int main() {
    std::vector<int> values{2, 4, 4, 8};
    auto found = std::lower_bound(values.begin(), values.end(), 4);
    if (found != values.end() && *found == 4) {
        std::cout << (found - values.begin()) << '\n';
    }
    auto missing = std::lower_bound(values.begin(), values.end(), 5);
    std::cout << (missing - values.begin()) << '\n';
    assert(missing != values.end() && *missing == 8);
}
Read the full lesson