title: C++ map iteration with deletion
body: <p>I couldn't find an instance of how to do this, so I was hoping someone could help me out. I have a map defined in a class as follows:</p>
  
  <pre><code>std::map&lt;std::string, TranslationFinished&gt; translationEvents;
  </code></pre>
  
  <p>TranslationFinished is a boost::function. I have a method as part of my class that iterates through this map, calling each of the functions like so:</p>
  
  <pre><code>void BaseSprite::DispatchTranslationEvents()
  {
      for(auto it = translationEvents.begin(); it != translationEvents.end(); ++it)
      {
          it-&gt;second(this);
      }
  }
  </code></pre>
  
  <p>However it's possible for a function called by <code>it-&gt;second(this);</code> to remove an element from the translationEvents map (usually itself) using the following function:</p>
  
  <pre><code>bool BaseSprite::RemoveTranslationEvent(const std::string &amp;index)
  {
      bool removed = false;
      auto it = translationEvents.find(index);
      if (it != translationEvents.end())
      {
          translationEvents.erase(it);
          removed = true;
      }
      return removed;
  }
  </code></pre>
  
  <p>doing this causes a debug assertion fail when the <code>DispatchTranslationEvents()</code> tries to increment the iterator. Is there a way to iterate through a map safely with the possibility that a function call during the iteration may remove an element from the map?</p>
  
  <p>Thanks in advance</p>
  
  <p>EDIT: Accidently C/Pd the wrong Remove Event code. Fixed now.</p>