Created
June 19, 2018 02:42
-
-
Save aruslan/cac3ef00e5bb45a84a18aeb50bfc81f8 to your computer and use it in GitHub Desktop.
an example of zip for N.K.
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 <iostream> | |
#include <iterator> | |
#include <tuple> | |
template <typename C1, typename C2> | |
struct zip_container { | |
C1& c1; C2& c2; | |
typedef std::tuple< | |
decltype(std::begin(c1)), decltype(std::begin(c2)) | |
> tuple; | |
struct iterator: public std::iterator< | |
std::input_iterator_tag, | |
tuple, | |
std::ptrdiff_t, | |
const tuple*, | |
const tuple&> { | |
tuple current; | |
explicit iterator(const tuple& t) : current(t) {} | |
iterator& operator++() { | |
++std::get<0>(current); | |
++std::get<1>(current); | |
return *this; | |
} | |
iterator operator++(int) { iterator cp = *this; ++(*this); return cp;} | |
bool operator==(iterator other) const { return current == other.current; } | |
bool operator!=(iterator other) const { return !(*this == other); } | |
tuple& operator*() {return current; } | |
}; | |
iterator begin() { return iterator(tuple(std::begin(c1), std::begin(c2))); } | |
iterator end() { return iterator(tuple(std::end(c1), std::end(c2))); } | |
zip_container(C1& c1, C2& c2) : c1(c1), c2(c2) {} | |
}; | |
template <typename C1, typename C2> | |
zip_container<C1, C2> zip(C1& c1, C2& c2) { | |
return zip_container<C1, C2>(c1, c2); | |
} | |
int main() { | |
int a[] { 1, 2, 3 }; | |
const char* b[] { "a", "b", "c" }; | |
for (auto& [av, bv] : zip(a,b)) { | |
std::cout << *av << ' ' << *bv << '\n'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment