title: How to avoid deleting priority_queue::top() despite a pop()?
body: <p>The interface of STL and boost priority queues includes</p>
  
  <pre><code>T const &amp;   top () const;
  void    pop ();
  </code></pre>
  
  <p>The latter deletes the top element.  But what if I want to keep using that element after I pop() it, and I want to avoid copying?  E.g., suppose I have a <code>priority_queue&lt;T&gt; pq</code>.  I'd like to write</p>
  
  <pre><code>const T&amp; first = pq.top();      
  pq.pop();
  const T&amp; second = pq.top();
  analyze(second);
  analyze(first);      // first *after* second
  </code></pre>
  
  <p>Unfortunately, as soon as I pop(), the first reference becomes invalid, so I get a segfault.  </p>
  
  <p>I'd prefer a solution like java's <code>nextElement()</code>, which  returns <code>top()</code> and does a <code>pop()</code>, but deletes the element only when it goes out of scope.  That way I don't need to keep track of what to <code>pop()</code> and when.  However, using <code>priority_queue&lt;shared_pointer&lt;T&gt; &gt;</code> doesn't seem to help, because taking a ref to a <code>shared_pointer</code> doesn't increase its use_count.  </p>
  
  <p>In case it matters, I prefer to use <code>boost::fibonacci_heap</code> for its efficient <code>push()</code>.  </p>
  
  <p>Any ideas or pointers?  Thanks!</p>