Created
August 5, 2022 08:11
-
-
Save dyigitpolat/dff2544e46b4f72e984323451834c5b0 to your computer and use it in GitHub Desktop.
co-traverse two ranges
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 <vector> | |
#include <array> | |
#include <algorithm> | |
void co_traverse(auto& range1, auto& range2, auto& binary_op) | |
{ | |
struct NullIterator | |
{ | |
int value{}; | |
NullIterator& operator++(){ return *this; } | |
int& operator*() { return value; } | |
}; | |
std::transform( | |
std::begin(range1), | |
std::end(range1), | |
std::begin(range2), | |
NullIterator{}, [&](auto& a, auto& b){ | |
binary_op(a, b); | |
return 0; | |
}); | |
} | |
void foo(int a, int b) | |
{ | |
std::cout << a << " " << b << "\n"; | |
} | |
int main() | |
{ | |
std::vector v1{1,2,3,4,5}; | |
std::array a1{6,7,8,9,10}; | |
co_traverse(v1, a1, foo); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment