Question

multimap offers the methods lower_bound and upper_bound. Both may return an iterator to a value with key greater than the desired, with lower_bound possibly yielding exactly the desired.

Now I want an iterator to a value where the key is strictly less the requested. If it were a map rather than multimap, this would be relatively simple to achieve as described here: Returning the greatest key strictly less than the given key in a C++ Map. But in a multimap, decrementing an iterator is not guaranteed to make it point to a strictly smaller key. So I would need to decrement repeatedly, until a smaller key is found. Not particularly nice.

Is there a more elegant way of doing this?

The keys will in general be floating-point.


My apologies, it turns out that you can actually do it with a single decrement. I just placed it wrong in my program, that was the real error.

Was it helpful?

Solution

lower_bound points to the smallest element greater than or equal to the argument (or end). Thus decrementing it once gives you the desired element (if it exists).

OTHER TIPS

AFAIK, lower/upper_bound will return iterator to FIRST element of this value, So you can decrease it

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top