Home > Enterprise >  C Primer (5th edition); Ch 19 - Algorithms: std::lower_bound
C Primer (5th edition); Ch 19 - Algorithms: std::lower_bound

Time:09-17

This is from C Primer (5th edition); Ch 19. "Appendix: algorithms":

lower_bound(beg, end, val)
lower_bound(beg, end, val, comp)

Returns an iterator denoting the first element such that val is not less than that element, or end if no such element exists.

upper_bound(beg, end, val)
upper_bound(beg, end, val, comp)

Returns an iterator denoting the first element

  • But I think lower_bound returns an iterator denoting the first element in the input sequence that is not less than val (greater than or equal to val) not the contrary ("...first element such that val is not less than that element"). Is it a mistake in the book?

CodePudding user response:

Is it a mistake in the book?

If you trust cppreference, then: Yes, it is a mistake:

std::lower_bound

Returns an iterator pointing to the first element in the range [first, last) that is not less than (i.e. greater or equal to) value, or last if no such element is found.

Or, if you don't trust that website, this Draft C 17 Standard has:

28.7.3.1 lower_bound       [lower.bound]



2     Returns: The furthermost iterator i in the range [first, last] such that for every iterator j in the range [first, i) the following corresponding conditions hold: *j < value or comp(*j, value) != false.

In this (later) online Draft Standard, it's §25.8.4.2.

  • Related