title: Erasing elements from unordered_map in a loop
body: <p>There are several answers on StackOverflow that suggest that the following loop is a fine way to erase elements from a <code>std::unordered_map</code> that satisfy some predicate <code>pred</code>:</p>
  
  <pre><code>std::unordered_map&lt;...&gt; m;
  auto it = m.begin();
  while (it != m.end())
  {
      if (pred(*it))
          it = m.erase(it);
      else
          ++it;
  }
  </code></pre>
  
  <p>I'm specifically interested in C++11 (as opposed to C++14), and the following ominous <a href="http://en.cppreference.com/w/cpp/container/unordered_map/erase" rel="noreferrer">note on cppreference.com</a> suggests that the above loop depends on undefined behavior and may not work in C++11 after all:</p>
  
  <blockquote>
    <p>The order of the elements that are not erased is preserved (this makes it possible to erase individual elements while iterating through the container) <strong>(since C++14)</strong></p>
  </blockquote>
  
  <p>See also <a href="http://wg21.cmeerw.net/lwg/issue2356" rel="noreferrer">Title 2356. Stability of erasure in unordered associative containers</a> which contains a requested wording change to <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf" rel="noreferrer">Working Draft N3797</a> item 14 on page 754 (the additional phrase beginning ", and preserve the relative order ...").</p>
  
  <blockquote>
    <p>This wording is relative to N3797.</p>
    
    <p>Modify [unord.req], p14 as indicated:</p>
    
    <p>-14- The insert and emplace members shall not affect the validity of references to container elements, but may invalidate all iterators to
    the container. The erase members shall invalidate only iterators and
    references to the erased elements, and preserve the relative order of
    the elements that are not erased.</p>
  </blockquote>
  
  <p>If my interpretation of the note from cppreference.com is correct, and the above loop depends on undefined behavior in C++11, what's the most efficient way to solve this problem in C++11?</p>