Reputation: 17197
In upper_bound
's binary predicate, the iterator value is passed as the second argument while in lower_bound
the iterator value is passed as the first argument.
What is the reasoning here? And does it matter if I remember this detail or not when writing my own binary predicates?
NOTE my reference is www.cplusplus.com (which I am told may not be the best reference) and I confirmed it by looking at the implementation in the stl
library shipped with VC++
.
Upvotes: 4
Views: 1981
Reputation: 302852
This is intentional. The reasoning is so you can use the same comparator for both algorithms. Consider the descriptions. 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.
and upper_bound
:
Returns an iterator pointing to the first element in the range [first, last) that is greater than value.
Consider that the standard comparator is <
. In order to implement both algorithms with only <
would require !(elem < value)
for one and value < elem
for the other. The inversion of the order of arguments just follows directly from that.
This shouldn't affect how you implement your Comparator though.
Upvotes: 7