samedi 11 juillet 2020

Having difficulty understanding this snippet of code

I came across this snippet of code and I have some difficulty understanding what it does and how to use it.

int fstTrue(function<bool(int)> f, int lo, int hi) {
    hi ++; assert(lo <= hi); // assuming f is increasing
    while (lo < hi) { // find first index such that f is true 
        int mid = (lo+hi)/2; 
        f(mid) ? hi = mid : lo = mid+1; 
    } 
    return lo;
}

If anyone can tell me what this is doing aswell as how I would use the function/output the value( since I dont understand what parameters function<bool(int)> takes) that would be great.

Thank you.

Aucun commentaire:

Enregistrer un commentaire