I was looking for a small code snippet that tells me how to use iterators in combination with erase (the latter makes the iterator invalid).
When browsing through the LyX sources, I came across the following code:
void PreviewLoader::Impl::remove(string const & latex_snippet) { ... InProgressProcesses::iterator ipit = in_progress_.begin(); InProgressProcesses::iterator ipend = in_progress_.end();
std::for_each(ipit, ipend, EraseSnippet(latex_snippet));
for (; ipit != ipend; ++ipit) { InProgressProcesses::iterator curr = ipit++; if (curr->second.snippets.empty()) in_progress_.erase(curr); } }
Question: Is the "for" statement correct? ipit seems to be increased twice with each iteration. Is this loop a safe way to cope with iterators?
Kind regards,
Michael