transform() vs generate()!!!

Aug 22, 2015 at 6:46pm
What is the difference between the transform() and the generate() algorithms in the STL?
Aug 22, 2015 at 7:00pm
Aug 22, 2015 at 9:34pm
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?
Aug 22, 2015 at 10:29pm
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 Aug 22, 2015 at 10:31pm
Aug 30, 2015 at 12:48am
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 Aug 30, 2015 at 12:49am
Aug 30, 2015 at 1:02am
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.