title: How to remove every multiple of 2 element in a vector by index
body: <p>I am trying to remove every 2nd element from an integer vector in C++</p>
  
  <pre><code>int n = 10001;    
  std::vector&lt;int&gt; sieve;    
  
  for (int i = 2; i &lt;= n; ++i)
      sieve.push_back(i);
  
  
  for (int i = 2; i &lt; sieve.size(); i += 2)
      sieve.erase(sieve.begin() + i);
  </code></pre>
  
  <p>I want to remove every 2nd element from the vector ( which would be multiples of 2 in range from 2-10001). The output is this:</p>
  
  <pre><code>// index, value
  0 2
  1 3
  2 5
  3 6
  4 8
  5 9
  6 11
  7 12
  8 14
  9 15
  10 17
  ....
  </code></pre>
  
  <p>So it removes the 4, 10, and 16 as expected. However there are still many multiples of 2 still in there, ie 6, 8, 12, 14</p>
  
  <p>EDIT Solved using method @Retired Ninja posted in comments. (Do it in reverse)</p>
  
  <pre><code>for (int i = 2; i &lt;= n; i++)
      sieve.push_back(i);
  
  for (int i = sieve.size(); i &gt;= p; i -= p)
      sieve.erase(sieve.end() - i);
  </code></pre>