Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Doing this eagerly and only with copying is pretty easy:</p> <pre><code>#include &lt;vector&gt; #include &lt;tuple&gt; #include &lt;algorithm&gt; template&lt;class... Ts&gt; std::vector&lt;std::tuple&lt;Ts...&gt;&gt; zip(std::vector&lt;Ts&gt; const&amp;... vs){ auto lo = std::min({vs.size()...}); std::vector&lt;std::tuple&lt;Ts...&gt;&gt; v; v.reserve(lo); for(unsigned i = 0; i &lt; lo; ++i) v.emplace_back(vs[i]...); return v; } </code></pre> <p><a href="https://ideone.com/wyVCwj">Live example.</a></p> <p>With perfect forwarding and allowing moves out of the vector, it becomes just a bit more complicated, mostly due to the helpers:</p> <pre><code>#include &lt;vector&gt; #include &lt;tuple&gt; #include &lt;algorithm&gt; #include &lt;type_traits&gt; template&lt;class T&gt; using Invoke = typename T::type; template&lt;class T&gt; using Unqualified = Invoke&lt;std::remove_cv&lt;Invoke&lt;std::remove_reference&lt;T&gt;&gt;&gt;&gt;; template&lt;class T&gt; using ValueType = typename Unqualified&lt;T&gt;::value_type; template&lt;class T&gt; T const&amp; forward_index(std::vector&lt;T&gt; const&amp; v, unsigned i){ return v[i]; } template&lt;class T&gt; T&amp;&amp; forward_index(std::vector&lt;T&gt;&amp;&amp; v, unsigned i){ return std::move(v[i]); } template&lt;class... Vs&gt; std::vector&lt;std::tuple&lt;ValueType&lt;Vs&gt;...&gt;&gt; zip(Vs&amp;&amp;... vs){ auto lo = std::min({vs.size()...}); std::vector&lt;std::tuple&lt;ValueType&lt;Vs&gt;...&gt;&gt; v; v.reserve(lo); for(unsigned i = 0; i &lt; lo; ++i) v.emplace_back(forward_index(std::forward&lt;Vs&gt;(vs), i)...); return v; } </code></pre> <p><a href="https://ideone.com/tnVX2r">Live example.</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload