title: Splitting a double vector into equal parts
body: <p>Greetings,</p>
  
  <p>Any input on a way to divide a std::vector into two equal parts ? I need to find the smallest possible difference between |part1 - part2|.</p>
  
  <p>This is how I'm doing it now, but from what you can probably tell it will yield a non-optimal split in some cases.</p>
  
  <pre><code>auto mid = std::find_if(prim, ultim, [&amp;](double temp) -&gt; bool
  {
      if(tempsum &gt;= sum)
          return true;
  
      tempsum += temp;
      sum -= temp;
      return false;
  });
  </code></pre>
  
  <p>The vector is sorted, highest to lowest, values can indeed appear twice.
  I'm not expecting part1 and part2 to have the same numbers of elements, but sum(part1) should be as close as possible to sum(part2)</p>
  
  <p>For example if we would have { 2.4, 0.12, 1.26, 0.51, 0.70 }, the best split would be { 2.4, 0.12 } and { 1.26, 0.51, 0.70 }.</p>
  
  <p>If it helps, I'm trying to achieve the splitting algorithm for the Shannon Fano encoding.</p>
  
  <p>Maybe this will help you guys understand my question better <a href="http://en.wikipedia.org/wiki/Shannon%E2%80%93Fano_coding#Example" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Shannon%E2%80%93Fano_coding#Example</a></p>
  
  <p>Any input is appreciated, thanks!</p>