title: How do you iterate on first N elements of a container?
body: <p>I have created a set of pairs in C++ to hold potentially colliding pairs of particles for a simple particle simulation as follows:</p>
  
  <pre><code>std::set&lt;std::pair&lt;int, int&gt;&gt; uniquePairs;
  </code></pre>
  
  <p>Once populated I want to iterate through it ONLY for the live particles in the simulation to check the distances between them for interaction later. I can iterate through the complete set like so...</p>
  
  <pre><code>std::set&lt;std::pair&lt;int,int&gt;&gt;::iterator it;
  
  for (it = uniquePairs.begin(); it != uniquePairs.end(); ++it) 
  {
      std::cout &lt;&lt; it-&gt;first &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>BUT, this will throw up a vector 'out of range' error since I would be trying to access particles that are not yet alive. Therefore I want to be able to access the list UP TO the same number of live particles .. i.e N = particles.size().</p>
  
  <p>Please help,</p>
  
  <p>Many thanks!</p>