transform() vs generate()!!!

What is the difference between the transform() and the generate() algorithms in the STL?
So the only difference is that std::transform applies the result of the operation in a different t iterator range?

What about if you use the source range the same as the destination range? Would that be equivalent to using std::generate?
No. std::generate is the same as
1
2
for each x in xs
    x = function()

std::transform is the same as
1
2
3
assert(xs.size() == ys.size())
for each i in xs.indices()
    ys[i] = function(xs[i])

std::generate expects a nullary function, while std::transform expects a unary function.

PS: When I say "function" in this context, I'm really saying "function-like object".
Last edited on
PS: When I say "function" in this context, I'm really saying "function-like object"

I think you mean "functor" or "function object", am I correct? (or anything that can be called)
Last edited on
Well, no, because a function is not a functor. You could say f is a function-like object iff f(/*...*/) compiles.
Topic archived. No new replies allowed.