title: performing vector intersection in C++
body: <p>I have a vector of vector of unsigned. I need to find the intersection of all these vector of unsigned's for doing so I wrote the following code:</p>
  
  <pre><code>int func()
  {
     vector&lt;vector&lt;unsigned&gt; &gt; t;
     vector&lt;unsigned&gt; intersectedValues;
     bool firstIntersection=true;
     for(int i=0;i&lt;(t).size();i++)
     {
         if(firstIntersection)
         {
             intersectedValues=t[0];
             firstIntersection=false;
         }else{
             vector&lt;unsigned&gt; tempIntersectedSubjects;                                                              
             set_intersection(t[i].begin(),
                    t[i].end(), intersectedValues.begin(),
                    intersectedValues.end(),
                    std::inserter(tempIntersectedSubjects, tempIntersectedSubjects.begin()));
             intersectedValues=tempIntersectedSubjects;
         }         
         if(intersectedValues.size()==0)
             break;
     }               
  }
  </code></pre>
  
  <p>Each individual vector has 9000 elements and there are many such vectors in "t". When I profiled my code I found that set_intersection takes the maximum amount of time and hence makes the code slow when there are many invocations of func(). Can someone please suggest as to how can I make the code more efficient.</p>
  
  <p>I am using: gcc (GCC) 4.8.2 20140120 (Red Hat 4.8.2-15)</p>
  
  <p>EDIT: Individual vectors in vector "t" are sorted.</p>