title: Operator overload or comparison function in C++ priority queue
body: <p>I am writing a program in C++ and I would like to define priority queue of one of my class. I need it to compare objects by one of the class member variable. I have used an operator&lt; overload, but I know there is a second way to achieve this goal - special function defined with queue definition. Which way is better, more esthetic and more efficient? And how to write such a function?</p>
  
  <p>I have done it as follows:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;queue&gt;
  using namespace std;
  
  class Human {
  
      public:
          string name;
          int age;
          Human(string name, int age);
  };
  
  Human::Human(string name, int age) : name(name), age(age) {}
  
  bool operator&lt;(Human a, Human b) {return a.age &lt; b.age ? true : false;}
  
  int main() {
  
      Human p1("Child",5);
      Human p2("Grandfather",70);
      Human p3("Older son",20);
      Human p4("Father",40);
      Human p5("Younger son",10); 
  
      priority_queue&lt;Human&gt; Q;
  
      Q.push(p1);
      Q.push(p2);
      Q.push(p3);
      Q.push(p4);
      Q.push(p5);
  
      while(!Q.empty()) {
  
          cout &lt;&lt; "Name: " &lt;&lt; Q.top().name &lt;&lt; ", age: " &lt;&lt; Q.top().age &lt;&lt; endl;
          Q.pop();
      }
      return 0;
  }
  </code></pre>