Skip to content

Instantly share code, notes, and snippets.

@Trass3r
Created May 18, 2015 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Trass3r/8bf8418f0d8cd80be14f to your computer and use it in GitHub Desktop.
Save Trass3r/8bf8418f0d8cd80be14f to your computer and use it in GitHub Desktop.
C++ range zip
#include <boost/iterator/zip_iterator.hpp>
#include <boost/range.hpp>
template <typename... T>
auto zip(T&&... containers) -> boost::iterator_range < boost::zip_iterator<decltype(boost::make_tuple(std::begin(containers)...))> >
{
auto&& zip_begin = boost::make_zip_iterator(boost::make_tuple(std::begin(containers)...));
auto&& zip_end = boost::make_zip_iterator(boost::make_tuple(std::end(containers)...));
return boost::make_iterator_range(zip_begin, zip_end);
}
#include <vector>
void test()
{
std::vector<int> l = {1, 2, 3, 4};
std::vector<int> r = {2, 3, 4, 5};
for (auto p : zip(l, r))
{
int h = p.get<0>();
int t = p.get<1>();
assert(h + 1 == t);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment