title: elegant way to remove all elements of a vector that are contained in another vector?
body: <p>While looking over some code I found loopy and algorithmically slow implementation of std::set_difference
  :</p>
  
  <pre><code> for(int i = 0; i &lt; a.size(); i++)
   {
    iter = std::find(b.begin(),b.end(),a[i]);
    if(iter != b.end())
    {
       b.erase(iter);
    }
   }
  </code></pre>
  
  <p>It can be easily replaced with sort(vectors are not sorted) + set_difference, but that requires allocation of new memory(see my recent Q <a href="https://stackoverflow.com/questions/21191631/can-output-of-set-difference-be-stored-in-first-input">Can output of set difference be stored in first input?</a> why it cant be done "inplace").<br>
  So my solution would be something like:</p>
  
  <pre><code>sort(a.begin(), a.end());
  for(size_t i = 0; i &lt; b.size(); i++)
  {
   if (binary_search(a.begin(), a.end(), b[i]))
   {
       swap(b[i], b[b.size()-1]); //remove current element by swapping with last
       b.pop_back();     // and removing new last by shrinking
   }
  }
  </code></pre>
  
  <p>can it be done more elegantly?<br>
  elegant is subjective so within scope of this Q is defined as clearer code(ideally something from STL algorithms but I think it cant be done) but with no memory allocation and no increase in alg complexity.</p>