title: Calling erase() on set during iteration
body: <p>I have the following code:</p>
  
  <pre><code>#include&lt;bits/stdc++.h&gt;
  using namespace std;
  
  int main()
  {
      set&lt;string&gt; S;
      S.insert("item1");
      S.insert("item2");
      S.insert("item3");
      S.insert("item4");
      int i=0;
      for (set&lt;string&gt;::iterator it = S.begin(); it != S.end(); it++)
          {
              string temp = *it;
              if (i++%2)
              {
                  S.erase(temp); // Causes Seg Fault on next iteration
              }
          }
      cout&lt;&lt;"Items Removed\n";
      return 0;
  }
  </code></pre>
  
  <p>The above code tries to remove elements from the set based on a trivial condition. It fails on my system when compiled with mingw-w64 (gcc 7.1.0 x86_64-posix-seh-rev0) with a segmentation fault. </p>
  
  <p>Now I assumed this was because <code>erase()</code> invalidates the iterator to the current element, thus causing <code>it++</code> to fail. But I'm confused why this works fine on all the online IDEs I've tried (<a href="http://repl.it/" rel="nofollow noreferrer">Repl.it</a>, <a href="https://ideone.com/" rel="nofollow noreferrer">IdeOne</a>, <a href="https://www.codechef.com/ide" rel="nofollow noreferrer">CodeChef</a>, <a href="http://coliru.stacked-crooked.com/" rel="nofollow noreferrer">Coilru</a>, <a href="http://cpp.sh/" rel="nofollow noreferrer">Cpp.sh</a>). Thoughts?</p>