I wouldn't change this to use one of the algorithms unless you have a compiler that supports lambdas. It's completely clear as written. Even if your compiler did support lambdas, I'd probably not change this code.
One relatively straightforward option would be to write a flattening iterator. I wrote one for demonstration in an answer to another question.
If you really want a one-liner and can use bind
(boost::bind
from Boost, std::tr1::bind
from TR1, and std::bind
from C++0x will all work), then here is how that would look. I warn you in advance: it is horrible.
Edit: Technically this is also illegal. The type of a Standard Library member function is unspecified, so you cannot (portably or correctly) take the address of such a member function. If you could correctly take the address of a Standard Library member function, this is what it would look like:
typedef std::vector<int>::iterator (std::vector<int>::*IteratorGetter)();
std::for_each(int_vectors.begin(), int_vectors.end(),
std::bind(
std::bind(
&std::vector<int>::insert<std::vector<int>::iterator>,
&ints,
std::bind((IteratorGetter)&std::vector<int>::end, &ints),
_1,
_2
),
std::bind((IteratorGetter)&std::vector<int>::begin, _1),
std::bind((IteratorGetter)&std::vector<int>::end, _1)
)
);
(Yes, that is technically one "line of code" as it is a single statement. The only thing I have extracted is a typedef for the pointer-to-member-function type used to disambiguate the overloaded begin
and end
functions; you don't necessarily have to typedef this, but the code requires horizontal scrolling on Stack Overflow if I don't.)