I can't understand this given portion of the Dijkstra algorithm. I want to understand this portion of code line by line.
The code:
bool operator < (const DATA &p) const { return p.dist > dist; }
I have the basic knowledge of c/c++ code.
I can't understand this given portion of the Dijkstra algorithm. I want to understand this portion of code line by line.
The code:
bool operator < (const DATA &p) const { return p.dist > dist; }
I have the basic knowledge of c/c++ code.
bool operator < (const DATA &p) const {
return p.dist > dist;
}
This is less than <
operator overloading.
You are passing DATA &p
prefixed by const
which means p
is passed by reference and it can't be modified or altered inside the function.
The function started with const {
means there will be no write/modify operation inside the method.
p.dist > dist
means after pushing into priority_queue
, comparing between two Data
will follow this criteria - when Data
having smaller dist
will be appeared first in the priority queue than Data
with longer dist
. This sounds contradictory but this is true because priority_queue
is by-default a max heap.