jeudi 1 septembre 2016

c++ primer Binary Search by iterator

The quesiton is in the comment!

Code:

auto beg = text.begin(), end = text.end();  
auto mid = text.begin() + (end - beg) / 2;

while(mid != end && *mid != key)   
{

    if (key < *mid)
        end = mid;   //why not end = mid - 1??
    else
        beg = mid + 1;
    mid = beg + (end - beg) / 2;
}

This question have stuck me for a long time , thanks for helping me !

Aucun commentaire:

Enregistrer un commentaire