title: How to erase all elements of STL deque one by one?
body: <p>I am facing a weird issue related to erasing elements in a STL list while iterating it. I am aware of the fact that erasing an element invalidates the iterator. So, either </p>
  
  <p><code>"testQueue.erase(it++);"</code> OR <code>"it = testQueue.erase(it);"</code></p>
  
  <p>should be used.</p>
  
  <p>In order to simplify my problem, I tried to remove all elements while iterating a list. However, when using the former mechanism I am getting Segfault.</p>
  
  <p>For your reference below is the complete code.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;queue&gt;
  #include &lt;string&gt;
  
  int main()
  {
      std::string d1 = "d1";
      std::string d2 = "d2";
      std::string d3 = "d3";
  
      std::deque&lt;std::string&gt; testQueue;
      testQueue.push_back(d1);
      testQueue.push_back(d2);
      testQueue.push_back(d3);
  
      std::deque&lt;std::string&gt;::iterator it = testQueue.begin();
      for(; it!=testQueue.end(); ++it){
          std::cout &lt;&lt; (*it) &lt;&lt; std::endl;
      }
  
      std::cout &lt;&lt; "========" &lt;&lt; std::endl;
  
      it = testQueue.begin();
      while(it!=testQueue.end()){
          testQueue.erase(it++); // This causes Segfault
          //it = testQueue.erase(it); // This one works just fine.
      }
      std::cout &lt;&lt; "========" &lt;&lt; std::endl;
      std::cout &lt;&lt; "========" &lt;&lt; std::endl;
      std::cout &lt;&lt; "========" &lt;&lt; std::endl;
      std::cout &lt;&lt; "========" &lt;&lt; std::endl;
      std::cout &lt;&lt; "========" &lt;&lt; std::endl;
  
      it = testQueue.begin();
      for(; it!=testQueue.end(); ++it){
          std::cout &lt;&lt; (*it) &lt;&lt; std::endl;
      }
  
  
      return 0;
  }
  </code></pre>
  
  <p>I am clueless on why the post-increment approach is not working. Please note that the end goal is not to use this approach for erasing all elements. However, I want to understand what is the difference in the two approaches that I have mentioned.</p>