title: C++ Priority Queue with customized object
body: <p>I'm trying to understand how comparator works for priority queue, I did several tests:</p>
  
  <p>Test 1: Create comparator class, and use <code>priority_queue&lt;T, vector&lt;T&gt;, cmp&gt;</code></p>
  
  <p>It always works fine</p>
  
  <p>Test 2:</p>
  
  <pre><code>struct test {
      int a = 0;
  };
  
  bool operator&lt;(const test&amp; lhs, const test&amp; rhs) {
      return lhs.a &lt; rhs.a;
  }
  
  int main() 
  {
      priority_queue&lt;test&gt; pq;
  }
  </code></pre>
  
  <p>This works as expected.</p>
  
  <p>Test 3: Put test 2 inside a class</p>
  
  <pre><code>class T{
      struct test {
          int a = 0;
      };
  
      bool operator&lt;(const test&amp; lhs, const test&amp; rhs) {
          return lhs.a &lt; rhs.a;
      }
  };
  </code></pre>
  
  <p>Compiling Error:</p>
  
  <pre><code>'bool T::operator&lt;(const T::test&amp;, const T::test&amp;)' must take exactly one argument
  </code></pre>
  
  <p>It seems that the compiler thought that I was overloading the operator &lt; for class T. Is there any other ways to accomplish this if I really need the classes to be nested?</p>
  
  <p>Test 4: Overload operator&lt;</p>
  
  <pre><code>struct test {
      int a = 0;
      bool operator&lt;(const test&amp; rhs) {
          return a &lt; rhs.a;
      }
  };
  
  int main() 
  {
      vector&lt;test&gt; v;
      sort(v.begin(), v.end());   // No error
      set&lt;test&gt; s;                // No error
      priority_queue&lt;test&gt; pq;    // Compiling error
  }
  </code></pre>
  
  <p>Only priority_queue throws an error:</p>
  
  <pre><code>'passing 'const test*' as 'this' argument discards qualifiers'
  </code></pre>
  
  <p>I don't know why this works for sort and set but not for priority queue.</p>