title: C++ vector erase crashes program
body: <p>My program creates projectiles that move forward, if they go out of certain bounds, they are to be deleted from the vector that they are stored in. The vector stores xcord, ycord, zcord, and their respective directions.</p>
  
  <pre><code>int size = bullet.size();
          for(int i = 0; i &lt; size; i+=6)
          {
                  float xc = bullet[i];
                  float yc = bullet[i+1];
                  float zc = bullet[i+2];
                  float xd = bullet[i+3];
                  float yd = bullet[i+4];
                  float zd = bullet[i+5];
                  if(xc &gt; 100 || yc &gt; 10 || zc &gt; 100 || xc &lt; -100 || yc &lt; -10 || zc &lt; -100)
                  {
                        bullet.erase(bullet.begin()+i, bullet.begin()+i+5);
                        size=size-6;
                        i = i-6;
                  }
                  else
                  {
                       glEnable(GL_TEXTURE_2D);
                       glBindTexture ( GL_TEXTURE_2D, texture_id[3] );
                       glPushMatrix(); 
                       glTranslatef( xc+(xd/2), yc+(yd/2), zc+(zd/2));   //x y z coord of sphere
                       glRotatef( 0,0,1,0);
                       glRotatef( -80,1,0,0); 
                       glRotatef( 0,0,0,1); 
                       glScalef( 0.10f, 0.10f, 0.10f); 
                       gluQuadricTexture(quadric,1);
                       gluSphere(quadric,10.0,72,72); 
                       glPopMatrix();
                       glDisable(GL_TEXTURE_2D);
                       bullet[i] = xc+xd;
                       bullet[i+1] = yc+yd;
                       bullet[i+2] = zc+zd;
                  }
          }
  </code></pre>
  
  <p>But when a "bullet" goes out of bounds my program seems to crash. Any ideas?</p>
  
  <hr>
  
  <p>Well changing </p>
  
  <pre><code>bullet.erase(bullet.begin()+i, bullet.begin()+i+5);
  </code></pre>
  
  <p>to</p>
  
  <pre><code>bullet.erase(bullet.begin()+i, bullet.begin()+i+6);
  </code></pre>
  
  <p>seems to have fixed it</p>
  
  <hr>
  
  <p>For those interested</p>
  
  <pre><code>      bullet.push_back(xpos);
        bullet.push_back(0.0f);
        bullet.push_back(zpos);
        bullet.push_back(nxpos);
        bullet.push_back(nypos);
        bullet.push_back(nzpos);
  </code></pre>
  
  <p>happens whenever the mouse is clicked</p>