I come from the Java world and am not too experienced with C++, so the following question has come up. I see that OutputIterator is used quite extensively. What I have seen so far is that people use an inserter, such as std::back_inserter.
Is it not possible to somehow provide a lambda which is called for each element instead of recording the elements in a container?
Instead of
std::vector<int> my_vector;
set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), std::back_inserter(my_vector));
something like
set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), to_iterator([](int x) {
    std::cout << x;
}));
                Boost has function_output_iterator for this purpose:
Live On Coliru
#include <boost/function_output_iterator.hpp>
#include <iostream>
#include <set>
int main() {
    std::set<int> s1{ 1, 2, 3 }, s2{ 3, 4, 5 };
    set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
                     boost::make_function_output_iterator([](int x) { std::cout << x; }));
}
Prints:
3
It shouldn't be too hard to write a simplistic version of this (although I'd prefer to use boost::iterator_facade<> myself so you'd still be stuck with boost)
Is it not possible to somehow provide a lambda which is called for each element instead of recording the elements in a container?
A lambda, by itself, won't be enough as an argument to std::set_intersection. You'll need wrap it in a helper class (functor) that supports the requirements of an OutputIterator.
The crucial operators that an OutputIterator must support are *iter, iter++, and ++iter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With