title: Erasing an element from a vector using erase
body: <p>So I wanted to do thing like mentioned above. And I came up with a brilliant idea when it comes to a usual iteration, third part of the method. But I don't know how to deal with the problem when I have a loop inside a loop. And yes, I know it's caused by skipping elements while erasing.</p>
  
  <pre><code>int Collision::missleCollision(vector &lt;Missle*&gt; &amp;missle_vector, vector &lt;Enemy*&gt; &amp;enemy_vector,
                                 vector &lt;Obstacle*&gt; &amp;obstacle_vector, bool G)
  {
      int hit=0;
      for (auto it=missle_vector.begin(); it!=missle_vector.end(); ++it)
      {
          for (auto jt=enemy_vector.begin(); jt!=enemy_vector.end(); ++jt)
          {
              double x, y;
              x=(*jt)-&gt;getX()-(*it)-&gt;getX();
              y=(*jt)-&gt;getY()-(*it)-&gt;getY();
              if (x&lt;64 &amp;&amp; x&gt;-151 &amp;&amp; y&lt;14 &amp;&amp; y&gt;-103)
              {
                  delete *it;
                  it=missle_vector.erase(it);
                  delete *jt;
                  jt=enemy_vector.erase(jt);
                  hit++;
              }
          }
      }
  
      if(G){
          for (auto it = missle_vector.begin(); it!=missle_vector.end(); ++it)
          {
              for (auto jt = obstacle_vector.begin(); jt!=obstacle_vector.end(); ++jt)
              {
                  double x, y;
                  x=(*jt)-&gt;getX()-(*it)-&gt;getX();
                  y=(*jt)-&gt;getY()-(*it)-&gt;getY();
                  if (x&lt;64 &amp;&amp; x&gt;-61 &amp;&amp; y&lt;14 &amp;&amp; y&gt;-61)
                  {
                      delete  *it;
                      it = missle_vector.erase(it);
                  }
              }
          }
      }
  
      for (auto it = missle_vector.begin(); it != missle_vector.end(); )
      {
          if ((*it)-&gt;getX() &gt; 1920)
          {
              delete *it;
              it = missle_vector.erase(it);
          }
          else
          it++;
      }
      return hit;
  }
  </code></pre>