title: Erasing elements from a vector
body: <p>I want to clear a element from a vector using the erase method. But the problem here is that the element is not guaranteed to occur only once in the vector. It may be present multiple times and I need to clear all of them. My code is something like this:</p>
  
  <pre><code>void erase(std::vector&lt;int&gt;&amp; myNumbers_in, int number_in)
  {
      std::vector&lt;int&gt;::iterator iter = myNumbers_in.begin();
      std::vector&lt;int&gt;::iterator endIter = myNumbers_in.end();
      for(; iter != endIter; ++iter)
      {
          if(*iter == number_in)
          {
              myNumbers_in.erase(iter);
          }
      }
  }
  
  int main(int argc, char* argv[])
  {
      std::vector&lt;int&gt; myNmbers;
      for(int i = 0; i &lt; 2; ++i)
      {
          myNmbers.push_back(i);
          myNmbers.push_back(i);
      }
  
      erase(myNmbers, 1);
  
      return 0;
  }
  </code></pre>
  
  <p>This code obviously crashes because I am changing the end of the vector while iterating through it. What is the best way to achieve this? I.e. is there any way to do this without iterating through the vector multiple times or creating one more copy of the vector?</p>