"Victor Bazarov"
Fraser Ross wrote: What is wrong with for_each? Josuttis has it in his table of modifying
algorithms.
'for_each' is designed for calling essentially a 'void' function. That
function can modify its argument, but the return value of that function
is not used by 'for_each'. 'transform', OTOH, takes the functor and
uses the return value of each invocation to assign to the object from
the other sequence.
Just try writing the uppercasing using for_each, you will have to create
a modifying functor for that.
The behaviour required is not present with the function toupper. I see
for_each as definitely being the right algorithm. I would rather make a
function or functor work than use the wrong algorithm.
A missing algorithm is one that modifies a range using a second range and a
binary operation:
template <typename ForwardIterator_tp, typename InputIterator_tp, typename
BinaryOperation_tp>
BinaryOperation_tp transform_each(ForwardIterator_tp first1,
InputIterator_tp const last1, InputIterator_tp first2,
BinaryOperation_tp binaryOp) {
while (first1 != last1) {
binaryOp(*first1, *first2);
++first1, ++first2;
}
return binaryOp;
}
Fraser.