A variadic zip() might make implementing other variadic algorithms easy in terms of existing stl algorithms.
It might also be useful on its own.
The function would take variadic number of ranges, and return a range that iterates over all the input ranges, advancing all the input range iterators at each step, and yielding a tuple of values when dereferencing. The zipped range would only iterate until the shortest input range is consumed.
For example:
auto a = std::vector{1, 2, 3, 4};
auto b = std::vector{1, 2};
auto zipped = zip(a, b);
auto it = zipped.begin();
*it == std::tuple{*a.begin(), *b.begin()};
++it;
*it == std::tuple{*(a.begin()+1), *(b.begin() + 1)};
++it;
(it != zipped.end()) == false; // b only had 2 elements
I played around a bit with the idea and wrote a little poc implementation https://godbolt.org/z/9oGWs9.
Ps. boost seems to have something similar https://www.boost.org/doc/libs/1_75_0/libs/iterator/doc/zip_iterator.html
A variadic
zip()might make implementing other variadic algorithms easy in terms of existing stl algorithms.It might also be useful on its own.
The function would take variadic number of ranges, and return a range that iterates over all the input ranges, advancing all the input range iterators at each step, and yielding a tuple of values when dereferencing. The zipped range would only iterate until the shortest input range is consumed.
For example:
I played around a bit with the idea and wrote a little poc implementation https://godbolt.org/z/9oGWs9.
Ps. boost seems to have something similar https://www.boost.org/doc/libs/1_75_0/libs/iterator/doc/zip_iterator.html