title: Vector subscript out of range with nested for loops
body: <p>below is a snippet of code from a game I am writing in c++ with SFML.</p>
  
  <pre><code>    if (Keyboard::isKeyPressed(Keyboard::Space)) {
          if (fireClock.getElapsedTime().asMilliseconds() &gt; 150) {
          Bullet vecBullet(Vector2f(player-&gt;getPosition()), player-&gt;angle);
          bulletVector.push_back(vecBullet);
          sleep(milliseconds(5));
          fireClock.restart();
      }
  }
  
  if (Keyboard::isKeyPressed(Keyboard::A)) {
      Enemy vecEnemy;
      enemyVector.push_back(vecEnemy);
  }
  
  for (int i = 0; i &lt; bulletVector.size(); i++) {
      bulletVector[i].fire();
      bulletVector[i].draw();
  }
  for (int i = 0; i &lt; enemyVector.size(); i++) {
      enemyVector[i].draw();
  }
  
  for (int i = 0; i &lt; bulletVector.size(); i++) {
      if (bulletVector[i].getPosition().x &gt; 2000 || bulletVector[i].getPosition().x &lt; -100 ||
          bulletVector[i].getPosition().y &gt; 1100 || bulletVector[i].getPosition().y &lt; -100) {
          bulletVector.erase(bulletVector.begin() + i);
      }
  }
  
  for (int y = 0; y &lt; bulletVector.size(); y++) {
      for (int x = 0; x &lt; enemyVector.size(); x++) {
          if (enemyVector.size() &gt; 0 &amp;&amp; bulletVector.size() &gt; 0) {
              if (enemyVector[x].checkColl(Bullet(bulletVector[y]))) {
                  bulletVector.erase(bulletVector.begin() + y);
                  enemyVector.erase(enemyVector.begin() + x);
              }
          }
      }
  }
  
  cout &lt;&lt; enemyVector.size() &lt;&lt; " " &lt;&lt; bulletVector.size() &lt;&lt; endl;
  </code></pre>
  
  <p>What happens is that once the bullet is shot it hits the enemy and sometimes it crashes and throws the following error:</p>
  
  <blockquote>
    <p>program: C:\WINDOWS\SYSTEM32\MSVCP140D.dll File: c:\program files
    (x86)\microsoft
    visualstudio\2017\community\vc\tools\msvc\14.13.26128\include\vector
    Line: 1806</p>
    
    <p>Expression: vector subscript out of range</p>
  </blockquote>
  
  <p>Any insights you can give me would be greatly appreciated,
  Thank you. </p>