Module 13 · binary_search, lower_bound, upper_bound and equal_range
Every Binary Search Call, One by One, With Its Cost
Free with an accountReading
Bob wants the position of each price on Amara's list, so he writes int pos = binary_search(...). He tries 120, 450 and 700, and every one is at "position 1". The program compiled, ran and printed numbers, which is the worst kind of wrong. Lesson 01 met the four calls once each. This lesson takes them one by one, with the form, the trap and the price of each.
Checking what this lesson needs for you…
What is inside Every Binary Search Call, One by One, With Its Cost
- binary_search: a bool, and only a bool
- lower_bound: the index, the end() check, and an insert that keeps order
- upper_bound: the first greater, and four counts
- equal_range: both bounds as a pair
- A list sorted high to low needs greater<int>() in the search too
- Pairs searched by .first, and which way round the comparator is called
- Plain arrays and a sorted vector<string>
- On a list: it compiles, it is right, and it walksIntermediate
- On a set or a map: call the memberIntermediate
- Every cost in one table