title: How do I sort two vectors containing relative entries, using STL?
body: <p>My function has two vector references as an input. Their entries are related. 
  For clarity, let's assume they are:</p>
  
  <pre><code>vector&lt;string&gt; firstname = { "Bjarne",     "Alexander",  "Dennis",   "James",   "James"   };
  vector&lt;string&gt; lastname  = { "Stroustrup", "Stepanov",   "Ritchie",  "Coplien", "Gosling" };
  </code></pre>
  
  <p>I want to sort them, find the unique entries, and remove the rest, using STL.</p>
  
  <p>I know I can copy them to an intermediate vector of pairs, <code>v</code>, then do the work</p>
  
  <pre><code>void my_func (vector&lt;string&gt; *firstname, vector&lt;string&gt; *lastname)
  {
     vector&lt;pair&lt;string,string&gt;&gt; v;
     for ( all i in firstname )
        v.push_back( (*firstname)[i], (*lastname)[i] ); // copy all entries
  
     sort(v.begin(), v.end());                          // clear duplicates
     v.erase(unique(v.begin(), v.end()), v.end());
  
     for ( all i in v ) {
         // copy back to firstname and lastname
     }
  }
  </code></pre>
  
  <p>but I was wondering if I can use STL to perform this without creating the whole temporary vector, <code>v</code>, or no other intermediate vector of similar size, because my input data have a very large number of entries.</p>
  
  <p>It looks like I could pass a temporary comparer object to <code>std::sort</code></p>
  
  <pre><code>struct ComparePersons
  {
     vector&lt;string&gt; *first_names;
     vector&lt;string&gt; *last_names;
  
     bool operator() (const string &amp;a, const string &amp;b) 
     {
         // get indexes of names
         int index_a = std::distance( first_names.begin(), &amp;a );
         int index_b = std::distance( first_names.begin(), &amp;b );
  
         // create temporary persons   
         pair&lt;string, string&gt; person_a ( 
                                    (*first_names)[index_a], 
                                    (*last_names)[index_a] );
  
         pair&lt;string, string&gt; person_b ( 
                                    (*first_names)[index_b], 
                                    (*last_names)[index_b] );
  
         // compare persons
         return person_a &lt; person_b;
     }
  };
  
  sort( first_names.begin(), first_names.end(), 
        ComparePersons(&amp;first_names, &amp;last_names) );
  </code></pre>
  
  <p>but I cannot think of how to make <code>sort</code> to swap the entries in both vectors.</p>
  
  <p>Any ideas on how to deal with these cases using STL ?</p>