If you look at the first possible implementation of std::transform
template<class InputIt, class OutputIt, class UnaryOperation> OutputIt transform(InputIt first1, InputIt last1, OutputIt d_first, UnaryOperation unary_op) { while (first1 != last1) { *d_first++ = unary_op(*first1++); } return d_first; }
This may not seem to be "safe."
However, with std::transform(str.begin(), str.end(),str.begin(), ::toupper);
d_first and first1 point to the same place, but this is not the same iterator!
There is no problem adding both of these iterators to a single statement.
Another implementation is something like this (from the MingW header file), which is equivalent but looks cleaner
template<class InputIt, class OutputIt, class UnaryOperation> OutputIt transform(InputIt first1, InputIt last1, OutputIt d_first, UnaryOperation unary_op) { for (; first1 != last1; ++first1, ++d_first) *d_first = unary_op(*first1); return d_first; }
Edited by John Bartholomew
source share