Created
May 18, 2015 10:49
-
-
Save Trass3r/8bf8418f0d8cd80be14f to your computer and use it in GitHub Desktop.
C++ range zip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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