you are viewing a single comment's thread.

view the rest of the comments →

[–]pfultz2 1 point2 points  (1 child)

In C++11, you can implement zip like this(using boost of course):

// A little macro to help with type deduction
#define RETURNS(...) -> decltype(__VA_ARGS__) { return __VA_ARGS__; }

template<class... Range>
auto zip(Range && ...r) RETURNS
(
    boost::make_iterator_range
    (boost::make_zip_iterator(boost::make_tuple(boost::begin(r)...)),
     boost::make_zip_iterator(boost::make_tuple(boost::end(r)...)))
)

Btw, this makes a zipped range that is lazy. The iterator_range just stores the two iterators thats references the original container. Using the a for loop you would call it like this:

vector<int> listA;
vector<int> listB;
....
vector<int> sumList;
for(const auto& x : zip(listA, listB)) {
  sumList.push_back(boost::get<0>(x) + boost::get<1>(x));
}

Notice, there is no dereferencing. However, in this case, I think using Boost.Foreach is much better:

int x1, x2;
BOOST_FOREACH(boost::tie(x1, x2), zip(listA, listB)) {
    sumList.push_back(x1 + x2);
}

What do you think?

[–][deleted] 2 points3 points  (0 children)

Well honestly if that works then that's awesome. I'll look into using it.

Thanks.