0

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.

waka
  • 3,362
  • 9
  • 35
  • 54
Mukit
  • 1
  • 3
  • 1
    It is called operator overload. It overloads `<` operator in code and this function is called when you do `a < b` where `a` and `b` have the same type of some class for example. You would implement this `bool operator...` overload in this class. http://en.cppreference.com/w/cpp/language/operators – unalignedmemoryaccess Oct 06 '17 at 06:14

1 Answers1

1
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.

Kaidul
  • 15,409
  • 15
  • 81
  • 150