Re: STL set lower_bound
On Mar 30, 9:52 am, Paavo Helde <pa...@nospam.please.ee> wrote:
mattg <gara.m...@gmail.com> kirjutas:
...
notation. As you all probably know a lower bound of a number
x in the reals should be a number y in the reals such that
y<=x. Guess the guys designing STL containers slept through
these classes.
Math and programming are two different things. For starters,
there is no datatype actually corresponding to real numbers.
I believe STL designers worked their design out very
carefully. If lower_bound were to return the "previous"
element, then what to return if there is none? There is no
concept of an iterator "before begin()". I guess they had a
choice of introducing yet another pseudovalue in addition to
end () iterator, or demanding all algorithm users to deal with
the special case when there is no "previous element". I guess
they preferred to return the "next" iterator and avoid the
problem entirely, so that only those users who actually need
the previous one are having to consider the special case.
The documentation at SGI site says that lower_bound "returns
the first position where value could be inserted without
violating the ordering". So it appers this is not lower bound
of existing elements, but lower bound of insertion places for
the new element.
It treats the argument as the "lower bound", and and returns the
first element in the set corresponding to that lower bound:-).
(Of course, then we have problems with upper_bound.)
Seriously, IIUC, the idea is that by iterating between
lower_bound(a) and upper_bound(b), you visit all elements >= a
and < b. In other words, you've defined a (possibly empty)
sequence in the usual iterator conventions. And that possibly
empty is important; it means that if a is not a member of the
set, lower_bound(a) must equal upper_bound(a).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34