I have a fairly large, sophisticated algorithm that uses a std::priority_queue
. In some cases, I want the queue to be a min-queue, and in others, a max-queue. So I have to provide a comparator type that does one or the other. Since the comparator is a template parameter, it is fundamentally part of the type of the std::priority_queue
. So everywhere the queue is referenced, the code must know the type.
One option, of course, is to provide a custom comparator that has state, and selects between ascending and descending on each invocation of operator()
, but I'm trying to avoid the performance overhead of having this branch for every comparison. The other alternative that I can see is to duplicate the entire algorithm, one for sorting ascending, and the other for descending.
Is there a better alternative to this problem?