title: Find the combination of two vector arrays which sums to maximum of all the other combinations in c++
body: <p>To find the maximum sum pair where one element is taken from one vector array and other element from second vector array .I found this code <a href="https://stackoverflow.com/questions/1546701/how-to-get-combination-of-two-array-vector-by-stl-algorithm#">here</a> but this fails when the size of the v1 and v2 both become 100000 as the output array size exceeds the limit and abort is called.</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;algorithm&gt;
  #include &lt;iostream&gt;
  #include &lt;iterator&gt;
  typedef int T;
  struct Fctor
  {
      typedef std::vector&lt;T&gt;::iterator Iterator;
      Iterator it1, it2, begin, end;
      Fctor(Iterator begin1, Iterator end1, Iterator begin2)
      {
          begin = begin1;
          end = end1;
          it1 = begin1;
          it2 = begin2;
      }
      T operator()()
      {
          // T result = f(*it1, *it2);
          T result = (*it1) * (*it2);
          if(++it1 != end) return result;
          it1 = begin;
          ++it2;
          return result;
      }
  };
  
  int main()
  {
      std::vector&lt;T&gt; v1; v1.push_back(1); v1.push_back(2);
      std::vector&lt;T&gt; v2; v2.push_back(3); v2.push_back(4); v2.push_back(5);
      std::vector&lt;T&gt; result(v1.size() * v2.size());
      Fctor fctor(v2.begin(), v2.end(), v1.begin());
      generate(result.begin(), result.end(), fctor);
      std::copy(result.begin(), result.end(),
          std::ostream_iterator&lt;T&gt;(std::cout, " "));
      std::cout &lt;&lt; std::endl;
      // =&gt; 3 4 5 6 8 10
  }
  </code></pre>