When I use std::priority_queue as a Min Heap for example, I'd need to do something like this:
struct MyType{
...
int data;
};
auto greaterThanCmp = [](const MyType& a, const MyType& b){ return a > b; };
std::priority_queue<MyType, std::vector<MyType>, greaterThanCmp> minHeap;
My question is: is there an advantage to providing the greaterThanCmp for a Min Heap or would a less than comparer works just as well, had the committee/implementer gone that route.
Or in other words, why did the standard choose one over the other?
I have tried implementing my MinHeap class with a lessThanCmp just for the heck of it. I tested my code, it works fine.
Thank you very much for your response in advance!
std::less
overstd::greater
? Also, are you looking atpriority_queue
in isolation, or would you include the other ordered containers (likestd::set
) in your question? – JaMiTcomparator
function simply decides the order of the elements.greaterThan
would allow the smallest element to appear first, whereaslessThan
allows the largest one to appear first. Standard chosestd::less
to be default one. – aepless
as the default comparator, and larger values "usually" have morepriority
. – L. F.