I'm thinking about a function with signature
template<typename ...Ts>
std::vector<std::tuple<Ts...>> join_vectors(std::vector<Ts>&&...) {
//...
};
but probably a more general one accepting any iterable instead of just std::vector
would be good. Probably it would have a signature like this?
template<template<typename> typename C, typename ...Ts>
C<std::tuple<Ts...>> join_vectors(C<Ts>&&...) {
// ...
};
However, I'm not at this level yet in C++ (despite doing the same in Haskell would be relatively easy), hence I seek for help.
Unfortunately, Range-v3's zip
is not at my disposal in this case. I'm tagging it because I think those interested in it are in a better position to help me.
For any indexable containers with
size
something like this is possible:If the containers have properly implemented move constructors, this solution will copy the containers passed as lvalues and move from rvalue ones. Very slight downside is that lvalue containers are copied whole first instead of only the individual elements.
Example [Godbolt]
Output
Please disregard the readability of the printing code ;)
I modeled it after python
zip
functionality. Note your initial proposal copies the vectors, so the output is a vector with the values moved from the parameters.Returning an iterable
Cs
is harder because you would have to specify how to insert elements into it, iterators cannot do it on their own.Getting it work with iterators (but returning still a vector) is a chore, but in theory also possible.