title: Keeping the first N elements of a std::vector<> and removing the rest
body: <p>I have a <code>std::vector&lt;int&gt;</code> variable in my <code>C++</code> application. The size of the vector is determined at runtime, but is typically about <code>1000</code>. </p>
  
  <p>I have sorted this vector (which works well), and after sorting, I would like to keep only the first <code>50</code> elements.</p>
  
  <p>I have tried:</p>
  
  <pre><code>kpts.erase(kpts.begin() + 50, kpts.end());
  </code></pre>
  
  <p>where <code>kpts</code> is my vector, and the performance is horrible! Presumably because of the way <code>erase</code> operates. </p>
  
  <p><strong>Is there a way to only keep the first <code>50</code> elements of a vector?</strong> It seems like it should be obvious, but I can't find a way to do this.</p>