title: Erasing from a std::vector while doing a for each?
body: <p>The proper way to iterate is to use iterators. However, I think by erasing, the iterator is invalidated.</p>
  
  <p>Basically what I want to do is:</p>
  
  <pre><code>for(iterator it = begin; it != end; ++it)
  {
      if(it-&gt;somecondition() )
      {
       erase it
      }
  
  }
  </code></pre>
  
  <p>How could I do this without v[i] method?</p>
  
  <p>Thanks</p>
  
  <pre><code>struct RemoveTimedEvent
  {
      bool operator()(const AguiTimedEvent&amp; pX, AguiWidgetBase* widget) const 
      {
          return pX.getCaller() == widget;
      }
  };
  
  void AguiWidgetContainer::clearTimedEvents( AguiWidgetBase* widget )
  {
      std::vector&lt;AguiTimedEvent&gt;::iterator it = std::remove_if(timedEvents.begin(),
          timedEvents.end(), RemoveTimedEvent());
      timedEvents.erase(it, timedEvents.end());
  
  }
  </code></pre>