title: Erasing elements in a multimap while iterating
body: <p>I'm writing a nodal path finding algorithm. I need to run through a multimap and delete elements out of it under certain conditions, but keep iterating through the multimap. Below is my code so far, it seems to work most of the time, but occasionally I get an error when doing nct_it++. Is it safe to erase the iterator pointer from the table before incrementing the iterator?</p>
  
  <pre><code>std::list&lt;SinkSourceNodeConn&gt;::iterator it;
  std::multimap&lt;SysNode*, SysNode*&gt;::iterator nct_it;
  SysNode* found_node = NULL;
  nct_it = node_conn_table.begin();
  while(nct_it != node_conn_table.end()) {
  
      // Find the node in the ever shrinking node connection table...
      if(nct_it-&gt;first == parent_node)
          found_node = nct_it-&gt;second;
  
      // Remove the table entry if we have found a node
      if(found_node) {
          // Search for the node in the expanded list. If it's not found, add it.
          bool found_the_node = false;
          for(it = m_sink_source_nodes_.begin(); it != m_sink_source_nodes_.end(); it++) {
              if(it-&gt;sink_source == sink_source &amp;&amp; it-&gt;node == found_node)
                  found_the_node = true;
          }
          if(!found_the_node) {
              recursion_list.push_back(found_node);
              recursion_list.unique();
              SinkSourceNodeConn ssnc;
              ssnc.node = found_node;
              ssnc.sink_source = sink_source;
              m_sink_source_nodes_.push_back(ssnc);
              if(found_node-&gt;GetPotential() &lt; sink_source-&gt;GetPotential())
                  found_node-&gt;SetPotential(sink_source-&gt;GetPotential());
          }
          found_node = NULL; // Unset the found node...
          node_conn_table.erase(nct_it);
          nct_it++;
      } else
          nct_it++;
  
  }
  </code></pre>