13

See: http://eel.is/c++draft/#ranges

Given two C++2a ranges (as in objects that conform to the ranges concept of the ranges library) a and b, of equal length, is there a way to zip them together such that:

for (const auto& [a,b] : zip(a,b))

does what you expect? That is, it returns a range that has something destructurable binding pairs:

(a.begin(), b.begin())
(a.begin()+1, b.begin()+1)
(a.begin()+2, b.begin()+2)
...
(a.end()-1, b.end()-1)
3
  • 1
    Zip what please?? Post a self contained question as required. Feb 15, 2019 at 20:57
  • 1
    What do you expect?
    – user2100815
    Feb 15, 2019 at 20:58
  • You mean something like Python zip? Feb 15, 2019 at 20:59

2 Answers 2

13

As you can see, there is no zip_view currently in C++20 (as of this writing).

It was being proposed in P1035R4 (along with a handful of other adapters), the previous version of which was favorable received in San Diego and seemed like it has a very reasonable chance of landing in C++20. There are open questions regarding proxy references, but I don't think that's specific to zip.


Those questions regarding proxy references ended up causing zip to get dropped from P1035 and it was not adopted for C++20. Instead, zip is being proposed for C++23 as part of P2321 (which additionally includes a description of the kinds of proxy reference changes I mentioned).

3
  • Why is the example for (auto xy = view::zip(x, y); auto [xi, yi] : xy) and not for (auto [xi, xi] : xy) ? Feb 15, 2019 at 21:26
  • 2
    @AndrewTomazos You mean why isn't it for (auto [xi, yi] : view::zip(x, y))? Just a matter of taste, the two are equivalent.
    – Barry
    Feb 15, 2019 at 21:38
  • Barry maybe update the answer to say that planned standard is now C++23 Jun 4, 2021 at 12:41
0

ranges v3 niebler has already made public a library with a lazy zip and some more general forms also zip_view and others. Implemented as a header only library so you can read the code. With some nice examples of uses. In c++ zip will probably return a tuple when asked. and indexing into that tuple will give you the values.

1
  • 1
    Yes, range-v3 is what std ranges in C++20 is based on... but it hasn't (yet) migrated zip. Aug 25, 2020 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.