Matt
Matt

Reputation: 133

Inconsistency in definition of STL upper_bound?

The documentation for upper_bound states:

...it attempts to find the element value in an ordered range [first, last)... upper_bound returns the furthermost iterator i in [first, last) such that, for every iterator j in [first, i), value < *j is false.

However, if we have a vector<int> v that contains the numbers 1, 2, and 3, calling upper_bound(v.begin(), v.end(), 5) will return v.end(). But based on the definition, v.end() is not in the range [v.begin, v.end()). There is no such iterator that fits the requirements in the definition. Is the definition just lazy in not explicitly stating what happens in this case?

Upvotes: 0

Views: 185

Answers (1)

ildjarn
ildjarn

Reputation: 62995

SGI documentation is not relevant – the C++ standard is what you should be reading. Quoting C++11 §25.4.3.2:

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

Upvotes: 11

Related Questions