title: How to iterate through a list while adding items to it
body: <p>I have a list of line segments (a <code>std::vector&lt;std::pair&lt;int, int&gt; &gt;</code> that I'd like to iterate through and subdivide.  The algorithm would be, in psuedocode:</p>
  
  <pre><code>for segment in vectorOfSegments:
      firstPoint = segment.first;
      secondPoint = segment.second;
      newMidPoint = (firstPoint + secondPoint) / 2.0
      vectorOfSegments.remove(segment);
      vectorOfSegments.push_back(std::make_pair(firstPoint, newMidPoint));
      vectorOfSegments.push_back(std::make_pair(newMidPoint, secondPoint));
  </code></pre>
  
  <p>The issue that I'm running into is how I can <code>push_back</code> new elements (and remove the old elements) without iterating over this list forever.</p>
  
  <p>It seems like the best approach may be to make a copy of this vector first, and use the copy as a reference, <code>clear()</code> the original vector, and then <code>push_back</code> the new elements to the recently emptied vector.</p>
  
  <p>Is there a better approach to this?</p>