title: Erasing element from Vector
body: <p>In C++, how can I delete an element from a vector?</p>
  
  <ol>
  <li>Delete it right from where it is, i.e. let the vector resize</li>
  <li>Swap the element to be deleted with the last element s.t. pop_back() can be used (which I hope doesn't involve copying everything around...)</li>
  </ol>
  
  <p>For (1), I've tried the following, but I'm not quite sure if it does what it is supposed to do (remove the item passed to removeItem() ), and it  doesn't seem very elegant:</p>
  
  <pre><code>vector&lt;Item*&gt; items;            
  // fill vector with lots of pointers to item objects (...)
  
  void removeItem(Item * item) {
      // release item from memory
      if (int i = getItemIdIfExists(item) != -1) {
          items.erase (items.begin()+i);
      }
  }
  
  int getItemIdIfExists(Item * item) {
      // Get id of passed-in Item in collection
      for (unsigned int i=0; i&lt;items.size(); i++) {
          // if match found
          if (items[i] == item)     return i;  
      }
      // if no match found
      return -1;
  }
  </code></pre>