title: How to efficiently delete elements from vector c++
body: <p>I have a vector of pair of vectors(V1,V2) called pairV1V2 of the following form:</p>
  
  <pre><code>(1,2,3),(938,462,4837) -&gt; (V1,V2)
  (3,9,13),(938,0472,944)
  (81,84,93),(938,84,845)
  </code></pre>
  
  <p>Then I need to retain the following:</p>
  
  <pre><code>(1,2,3),(938,462,4837) -&gt; (V1,V2)
  (3,9,13),(938,0472,944)
  (81,84,93),(84,845)
  </code></pre>
  
  <p>I need to start scanning pairV1V2 from the beginning and where ever any two V1's are not equal, there I need to delete the intersecting elements from V2. I wrote the following code for doing same. However, my code turns out to be very inefficient as my vector pairV1V2 is big and it has many elements in V2 (about a billion).</p>
  
  <pre><code>int main(int argc, char** argv) {
      std::vector&lt;std::pair&lt;std::vector&lt;unsigned&gt;, std::vector&lt;unsigned&gt; &gt; &gt; pairV1V2;
      std::vector&lt;std::pair &lt;std::vector&lt;unsigned&gt;,std::vector&lt;unsigned&gt; &gt; &gt;::iterator itm2,lm2=pairV1V2.end();
      for(std::vector&lt;std::pair &lt;std::vector&lt;unsigned&gt;,std::vector&lt;unsigned&gt; &gt; &gt;::iterator itm=pairV1V2.begin(), lm=pairV1V2.end(); itm!=lm; ++itm)
      {
          //Outer values
          vector&lt;unsigned&gt; outerV1=(*itm).first;
          vector&lt;unsigned&gt; outerV2=(*itm).second;
          sort(outerV2.begin(), outerV2.end());
          itm2=itm;
          itm2++;
          for(itm2;itm2!=lm2;++itm2)
          {
              vector&lt;unsigned&gt; innerV1=(*itm2).first;
              vector&lt;unsigned&gt; innerV2=(*itm2).second;
              vector&lt;unsigned&gt; setDiffV1;
              std::set_difference(innerV1.begin(), innerV1.end(), outerV1.begin(), outerV1.end(),
                                                        std::inserter(setDiffV1, setDiffV1.end()));            
              if(setDiffV1.size()==0) //check whether any two V1's are different
              {                 
                  sort(innerV2.begin(), innerV2.end());
                  if((itm-&gt;second.size()!=0)&amp;&amp;(itm2-&gt;second.size()!=0)){                                
                      std::vector&lt;unsigned&gt; delIntersectingElem;
                      std::set_intersection(outerV2.begin(),outerV2.end(),innerV2.begin(), innerV2.end(),
                                std::back_inserter(delIntersectingElem));
  
                     if(delIntersectingElem.size()!=0) //if there are intersecting V2's
                     {                    
                          for(std::vector&lt;unsigned&gt;::iterator its=(itm2-&gt;second).begin(),ls=(itm2-&gt;second).end();its!=ls;)
                          { 
                              //if *its is present in delIntersectingElem then delete it.
                              if(!(std::find(delIntersectingElem.begin(), delIntersectingElem.end(), (*its)) == delIntersectingElem.end()))
                              {
                                  (itm2-&gt;second).erase(its); //delete intersecting elements from inner v2
                                  ls--;
                              }else{
                                  ++its;
                              }
                          }                    
                      }
                  }
              } 
          }
      }    
      return 0;
  }
  </code></pre>
  
  <p>Can someone please help me in improving my present code -- it gives the correct answer (In the example I might have missed a few case for brevity-- but the code handles all of them) but is extremely slow (as diagonalised by perf). I'll be grateful if improvements are suggestion in my present piece of code. However, if the logic of the two codes are same, then a new algorithm is also acceptable</p>