title: Using std::deque::iterator (in C++ STL) for searching and deleting certain elements
body: <p>I have encountered a problem invoking the following code:</p>
  
  <pre><code>#include&lt;deque&gt;
  using namespace std;
  
  deque&lt;int&gt; deq = {0,1,2,3,4,5,6,7,8};
  
  for(auto it = deq.begin(); it != deq.end(); it++){
      if(*it%2 == 0)
          deq.erase(it);
  }
  </code></pre>
  
  <p>which resulted in a segmentation fault. After looking into the problem I found that the problem resides in the way the STL manages iterators for deques: if the element being erased is closer to the end of the deque, the iterator used to point to the erased element will now point to the NEXT element, but not the previous element as <code>vector::iterator</code> does. I understand that modifying the loop condition from <code>it != deq.end()</code> to <code>it &lt; deq.end()</code> could possibly solve the problem, but I just wonder if there is a way to traverse &amp; erase certain element in a deque in the "standard form" so that the code can be compatible to other container types as well.</p>