title: How can I delete an element from my vector while in a for loop C++
body: <pre><code>void foodAgainstCreatures(vector&lt;creature&gt;&amp; displayCreatures, vector&lt;Food&gt;&amp; displayFood)
  {
  bool hungry = true;
  int differenceInX = 0;
  int differenceInY = 0;
  for (unsigned int j = 0; j &lt; displayCreatures.size(); j++)
  {
      hungry = true;
      for (unsigned int i = 0; i &lt; displayFood.size(); i++)
      {
          differenceInX = displayCreatures[j].getXValue() - displayFood[i].getXValue();
          differenceInY = displayCreatures[j].getYValue() - displayFood[i].getYValue();
          if ((differenceInX &lt;= 5 &amp;&amp; differenceInX &gt;= -5) &amp;&amp; (differenceInY &lt;= 5 &amp;&amp; differenceInY &gt;= -5))
          {
              displayCreatures[j].addToEnergy(3);
              displayFood.erase(displayFood.begin() + i);
              hungry = false;
  
          }
          else
          {
              if (displayCreatures[j].getEnergy() == 0)
              {
                  displayCreatures.erase(displayCreatures.begin() + j);
              }
          }
  
      }
      if (hungry == true)
      {
          displayCreatures[j].addToEnergy(-1);
      }
  }
  }
  </code></pre>
  
  <p>//once a "creature" "eats" a "food" it gains "energy" which works fine what i need help with is then removing that "food" that has been "eaten" from the vector. can i do this within the loop or not? </p>