You can write your own set_union() function to call a Lambda, if the one provided in the stl is not (yet?) capable. It (avoiding having to store the output) will be very well adressed by views::set_union() possibly C++23 but I am not sure if it really is in store.
#include <vector>
#include <stdio.h>
using namespace std;
template <typename Range, typename F>
void set_union(const Range& a, const Range& b, F&& f)
{
for (int i=0, j=0; i<a.size() || j<b.size();)
{
if (i < a.size() && j<b.size())
{
if (a[i] < b[j])
{
f(a[i++]);
}
else if (a[i] > b[j])
{
f(b[j++]);
}
}
else if (i < a.size())
{
f(a[i++]);
}
else if (j < b.size())
{
f(b[j++]);
}
}
}
int main(int argc, char* argv[])
{
vector<int> a = {1, 2, 6}, b = {3, 4, 5, 7};
set_union(a, b, [](const int& i){printf("%d,", i);});
return 0;
}
