transform is for one-to-one conversion only. Custom insertion will not help you, as transform implemented something like this:
while (first1 != last1) { *d_first++ = unary_op(*first1++);
In fact, you need to write your own iterator for a to repeat each element twice, and then save the state in your functor to find out if you are in state f or state g . You can see how difficult it is.
Anything other than simple 1-1 conversions, you should just use the for loop:
for (const auto& x : a) { b.push_back(f(x)); b.push_back(g(x)); }
And even for simple 1-1 conversions, I think that a simple range-for expression wins too.
You can optionally write your own transform , which takes an arbitrary number of functors:
template <typename InIt, typename OutIt, typename... Functors> void transform(InIt first, InIt last, OutIt d_first, Functors... fs) { while (first != last) { apply(*first, d_first, fs...); first++; } }
with;
template <typename In, typename OutIt> void apply(const In&, OutIt ) { } template <typename In, typename OutIt, typename F, typename... Functors> void apply(const In& in, OutIt& out, F f, Functors... fs) { *out++ = f(in); apply(in, out, fs...); }
used as ( example ):
transform(a.begin(), a.end(), back_inserter(b), f, g);