title: std::set_union vector<pair<string,string>>
body: <p>I want to compute the union between two vectors:</p>
  
  <p><code>std::vector&lt;pair&lt;string,string&gt;&gt; p1;</code></p>
  
  <p><code>std::vector&lt;pair&lt;string,string&gt;&gt; p2;</code></p>
  
  <p>The problem is that why is the union being done on the second element instead of first?
  the size of the union should be equal to <strong>4</strong>. Is it possible to modify set_union so that the Union is done on the first element?</p>
  
  <hr>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  int main()
  {
      std::vector&lt;pair&lt;string,string&gt;&gt; p1;
      std::vector&lt;pair&lt;string,string&gt;&gt; p2;
      p1.push_back(make_pair("A","1"));
      p1.push_back(make_pair("B","2"));
      p1.push_back(make_pair("C","3"));;
      p2.push_back(make_pair("A","4"));
      p2.push_back(make_pair("B","5"));
      p2.push_back(make_pair("C","6"));
      p2.push_back(make_pair("D","7"));
      //sort vectors according to first element
      sort(p1.begin(), p1.end(), [](const pair&lt;string,string&gt;&amp; lhs, const pair&lt;string,string&gt;&amp; rhs) {return lhs.first &lt; rhs.first; });
      sort(p2.begin(), p2.end(), [](const pair&lt;string,string&gt;&amp; lhs, const pair&lt;string,string&gt;&amp; rhs) {return lhs.first &lt; rhs.first; });
      //initialize vectors
      std::vector&lt;pair&lt;string,string&gt;&gt; v(p1.size() + p2.size());
      std::vector&lt;pair&lt;string,string&gt;&gt;::iterator it;
      //compute union
      it=std::set_union(p1.begin(), p1.end(),p2.begin(), p2.end(), v.begin());
      v.resize(it-v.begin());
      //print size
      //size = 4
      cout &lt;&lt; v.size() &lt;&lt; endl;
      return 0;
  }
  </code></pre>