title: Recommend C++ container to hold top 20 minimum values
body: <p>In SQL there is the feature to say something like</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT TOP 20 distance FROM dbFile ORDER BY distance ASC
  </code></pre>
  
  <p>If my SQL is correct with, say 10,000 records, this should return the 20 smallest distances in my databse.</p>
  
  <p>I don't have a database. I have a 100,000-element simple array.</p>
  
  <p>Is there a C++ container, <a href="http://en.wikipedia.org/wiki/Boost_%28C%2B%2B_libraries%29" rel="nofollow">Boost</a>, <a href="http://en.wikipedia.org/wiki/Microsoft_Foundation_Class_Library" rel="nofollow">MFC</a> or <a href="http://en.wikipedia.org/wiki/Standard_Template_Library" rel="nofollow">STL</a> that provides simple code for a struct like</p>
  
  <pre><code>struct closest{
      int    ID;
      double distance;
      closest():ID(-1), distance(std::numeric_limits&lt;double&gt;::max( )){}
  };
  </code></pre>
  
  <p>Where I can build a sorted by distance container like</p>
  
  <pre><code>boost::container::XXXX&lt;closest&gt; top(20);
  </code></pre>
  
  <p>And then have a simple</p>
  
  <pre><code>top.replace_if(closest(ID,Distance));
  </code></pre>
  
  <p>Where the container will replace the entry with the current highest distance in my container with my new entry if it is less than the current highest distance in my container.</p>
  
  <p>I am not worried about speed. I like elegant clean solutions where containers and code do all the <em>heavy lifting</em>.</p>
  
  <p>EDIT. Addendum after all the great answers received.</p>
  
  <p>What I really would of liked to have found, due to its elegance. Is a sorted container that I could create with a container size limit. In my case 20. Then I could push or insert to my hearts content a 100 000 items or more. But. There is always a but. The container would of maintained the max size of 20 by replacing or not inserting an item if its comparator value was not within the lowest 20 values.</p>
  
  <p>Yes. I know now from all these answers that via programming and tweaking existing containers the same effect can be achieved. Perhaps when the next round of suggestions for the C &amp; C++ standards committee sits. We could suggest. <strong>Self sorting (which we kind of have already) and self size limiting containers.</strong></p>