If I have a map/multimap with keys inserted using a comparison function (functor), is it possible to use map::equal_range with a comparison function as well? For instance, if I have a std::map m, and keys:
sometype a.getVal == 101
sometype b.getVal == 112 <-first pair (using equal_range)
sometype c.getVal == 113
sometype d.getVal == 121 <-second pair (using equal_range)
and I wanted to get a range/set of keys 11* is that possible?
There is a good reason why you cannot use
map::equal_range()with a custom comparer: it would defeat the asociativity of the container. It can be done, but the algorithm would be O(n) instead of O(log(n)), since it would have to compare allmapelements regardless of their position.I think the best solution for your problem would be a custom function that uses
map::lower_bound()andmap::upper_bound()to find your interval limits. Something like: