Take the 2-minute tour ×

I just realize the standard doesn't guarantee the order of applying function callback in std::transform. And it doesn't allow the callback function or functor have side effect. But at the same time std::for_each actually guarantee the order.

One guess is the transform can using high performance algorithm which doesn't guarantee order, But O(N) is the best algorithm already.

So why the standard doesn't make the transform have the behavior as for_each from the view of apply callback function order? The user will benefit form this guarantee.

share|improve this question
O(N) doesn't tell you anything about the actual performance, i.e. it neglects constant factors. –  Oliver Charlesworth Jun 28 '13 at 3:28

1 Answer 1

up vote 3 down vote accepted

This non-restrictive definition allows for parallel computing. An implementation may choose to apply transform function using several threads. See also related question: STL algorithms and concurrent programming

Think of it as a semantic difference in algorithms (that is, that represents programmer's intent rather than being just another tool). With for_each you state that you need a sequential scan. With transform you state that you only need to aply a function to every item in the container, but you don't care how it will be done.

share|improve this answer
Thanks, that exactly answer my question –  ZijingWu Jun 30 '13 at 4:49

Your Answer


