title: C++ STL Interleave two ranges
body: <p>I did something to achieve interleaving two ranges. Here is the problem.</p>
  
  <p>Say I have two ranges <code>[a1, b1), call R1</code> and <code>[a2, b2) call R2</code>. For now, assume their <code>std::distance</code> results are the same, say <code>n</code>. I want to write an algorithm</p>
  
  <p><code>std::interleave(a1, b1, a2, b2, bool swap)</code> </p>
  
  <p>This will reorder the elements in a way that one element of R2 will be followed an element of R1, through all <code>2n</code> elements. Depending of the value of <code>swap</code>, it should also be reordered as one element of R1 is followed by an element of R2.</p>
  
  <p>I did this using an additional storage and two for loops. This solution is easy. I could also come up with an in-place solution probably, if I tried a little longer. But what I want is, I want to achieve this thing by assembling algorithms from the STL. How can you solve this problem using STL? It is better if the solution is in-place but I am also open to usage of additional storage, as long as we use algorithms from the STL.</p>
  
  <p>EDIT: The permutation of the elements in the given ranges should not be changed. In other words, this should actually be something like <code>stable_interleave</code>. This is why I couldn't come up with something that uses <code>std::merge</code>.</p>
  
  <p>APPLICATION: One application of this is the conversion of video and image formats from planar to non-planar or semi-planar. </p>