Please help me to understand compartor function in priority queue

I am trying to understand how the compartor functions work on standard container templates. I was working with std:prioirty_queue container. The explanation says that the elements in the queue follow strict weak ordering of elements. The max element is stored at the top of the queue by following this weak ordering. For my implementation, I wanted to change this container to store the minimum value instead of maximum value. I looked up online and came up with this piece of code
1
2
3
4
5
6
7
8
9
10
11
12
class edgeSort {
    bool reverse;
public:
    edgeSort(const bool& revparam = false){
        reverse = revparam;
    }
    inline bool operator () ( Edge &lhs, Edge &rhs) const { 
        return reverse ? (lhs.cost < rhs.cost) : (lhs.cost > rhs.cost); 
    }
};

std::priority_queue<Edge,std::vector<Edge>, edgeSort> myPQ;


I did this and the priority queue started storing minimal value at the top. I don't understand how overloading () operator and returning the false condition value would make minimal element to be stored at the top. Is there someone who can explain me what's happening?

Last edited on
Topic archived. No new replies allowed.