title: May I clear a priority_queue by clearing its underlying container?
body: <p>The following code inherits std::priority_queue and provides <code>clear()</code> which calls the internal <code>std::vector</code>'s <code>clear()</code> </p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;queue&gt;
  using namespace std;
  template&lt;class type&gt;
  struct mypq :public priority_queue&lt;type&gt; {
      void clear(){
          this-&gt;c.clear();
      }
  };
  mypq&lt;int&gt;pq;
  int main() {
      for(int i=0;i&lt;10;++i)
          pq.push(i);
      pq.clear();
      for(int j=-5;j&lt;0;++j)
          pq.push(j);
      while (!pq.empty()){
          cerr&lt;&lt;pq.top()&lt;&lt;endl;
          pq.pop();
      }
  }
  </code></pre>
  
  <p>When I tested it with g++, MSVC++ and clang, it produces the expected output:</p>
  
  <pre><code>-1
  -2
  -3
  -4
  -5
  </code></pre>
  
  <p>But I haven't seen any guarantee for this, i.e. clearing the internal vector will be the same as calling <code>pop()</code> when the priority_queue isn't empty. Although I know other ways to clear it such as swap or assign it using an empty priority_queue, I think if this code can work well, it would be more efficient since the allocated memory in the vector is reusable. So I wonder if this code is portable or won't always work?</p>