Search code examples
c++algorithmstloperator-overloadingpriority-queue

What "operator()" means in overloading operators method, used in priority_queue (STL) as comparator in C++?


I've read in a book of c++ algorithms about overloading operators and I have a problem understanding how overloading operator works in that case. This is the sequence of code:

struct cmp {
     bool operator () (const int &x, const int &y) const
     {
         return x % 17 < y % 17;
     } 
}; 

int main() {
     priority_queue<int, vector<int>, cmp> note;
     note.push(80); note.push(97); note.push(100); note.push(30); 

     while ( note.size() > 0 )
     {
         cout << note.top() << " ";
         note.pop();
     } 

    return 0; 
}

What I don't understand is this line of code:

bool operator () (const int &x, const int &y) const

Please someone help me!


Solution

  • What I don't understand is this line of code:

    bool operator () (const int &x, const int &y) const
    

    This line of code declares an operator overload for the operator (). This overload has two arguments, both of which are references to const int. It is declared const, and therefore it can be called through const lvalues. The operator returns bool value.