title: Data structure that always keeps n-best elements
body: <p>I need a data structure that always holds the <code>n</code> largest items inserted so far (in no particular order). </p>
  
  <p>So, if <code>n</code> is 3, we could have the following session where I insert a few numbers and the content of the container changes:</p>
  
  <pre><code>[]  // now insert 1
  [1] // now insert 0
  [1,0] // now insert 4
  [1,0,4] // now insert 3
  [1,4,3] // now insert 0
  [1,4,3] // now insert 3
  [4,3,3]
  </code></pre>
  
  <p>You get the idea. What's the name of the data structure? What's the best way to implement this? Or is this in some library?</p>
  
  <p>I am thinking to use a container that has a <code>priority_queue</code> for its elements (delegation), which uses the reverse comparison, so <code>pop</code> will remove the smallest element. So the <code>insert</code> function first checks if the new element to be inserted is greater than the smallest. If so, we throw that smallest out and push the new element.</p>
  
  <p>(I have a <code>C++</code> implementation in mind, but the question is language-agnostic nevertheless.)</p>