I have a vector of pair of vectors(V1,V2) called pairV1V2 of the following form:

(1,2,3),(938,462,4837) -&gt; (V1,V2)
(3,9,13),(938,0472,944)
(81,84,93),(938,84,845)


Then I need to retain the following:

(1,2,3),(938,462,4837) -&gt; (V1,V2)
(3,9,13),(938,0472,944)
(81,84,93),(84,845)


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).

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;
}


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


Question: Lens Cleaner Recommendations
Answer: Given that I already own these for cleaning the sensor in my DSLR, I use Eclipse optical cleaning solution, and Pec-Pad cleaning cloths.

Eclipse is an extremely clean filtered methanol solution, that leaves no residue on glass and other optics. Likewise, the Pec-Pads are a woven, lint-free disposable cloth that is excellent for cleaning optics. Unlike regular lens tissue, these are very absorbent, so you are not smearing solution around the lens. These are recommended as part of the 'copperhill method' of sensor cleaning, and a bottle/100 wipe pack will last you a lifetime of sensor cleaning, meaning there is plenty left over for critical lens cleaning.

Note that I use these products primarily in the home, prior to a shoot, or after a shoot. In the field, I use a lens brush, and a microfiber cloth for emergency purposes. I prefer to NOT touch the lens in any fashion in the field, but sometimes you can not avoid or prevent it.

Response to question below about blower: Yes I do use and recommend a blower, and it can indeed be a useful addition to your bag. I recommend the Giottos Rocket Blower, as it is inexpensive, and it has a filter on the intake, which helps reduce dust being blasted out of the blower onto your equipment. Note however that they can be kinda bulky, and its usually one of the first things to get tossed out of the bag if I am traveling light. Also, don't over think this: your mouth makes a workable blower in a pinch, and is dust free, though not necessarily liquid free (see cleaner above).