#include <iostream>
#include <vector>

int bsearch(const std::vector<int>& array, const int target) {
    int min = 0;
    int max = array.size() - 1;    
    while (max >= min) {
        int mid = (min + max) / 2;
        if (target == array[mid]) {
            return mid;
        } else if (target > array[mid]) {
            min = mid + 1;
        } else {
            max = mid - 1;
        }
    }
    return -1;
}

int main() {
    std::vector<int> array {1, 2, 3, 4, 5};
    
    for (int i = 0; i < array.size()+2; i++) {
        int res = bsearch(array, i);
        std::cout << res << std::endl;    
    }
    
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: