Skip to content

Instantly share code, notes, and snippets.

@dyigitpolat
Created August 5, 2022 08:11
Show Gist options
  • Save dyigitpolat/dff2544e46b4f72e984323451834c5b0 to your computer and use it in GitHub Desktop.
Save dyigitpolat/dff2544e46b4f72e984323451834c5b0 to your computer and use it in GitHub Desktop.
co-traverse two ranges
#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