title: Erasing items from an STL list
body: <p>I want to make a function which moves items from one STL list to another if they match a certain condition.</p>
  
  <p>This code is not the way to do it. The iterator will most likely be invalidated by the erase() function and cause a problem:</p>
  
  <pre><code>for(std::list&lt;MyClass&gt;::iterator it = myList.begin(); it != myList.end(); it++)
  {
    if(myCondition(*it))
    {
      myOtherList.push_back(*it);
      myList.erase(it);
    }
  }
  </code></pre>
  
  <p>So can anyone suggest a better way to do this ?</p>