File prototypes and possible implementations:
Version One:
template<classclassclass UnaryOperation>OutputIt transform(InputIt first1, InputIt last1, OutputIt d_first, UnaryOperation unary_op){ while (first1 != last1) { *d_first++ = unary_op(*first1++); } return d_first;}
first1
a pair, last1
a range of elements after a unary operation unary_op
is saved at the d_first
beginning of the position.
Version two:
Template<class InputIt1,class InputIt2, class Outputit,class binaryoperation>OutputitTransform(InputIt1 first1, InputIt1 last1, InputIt2 first2, Outputit D_first, binaryoperation binary_op){While(first1 ! = last1){*d_first++ = Binary_op(*first1+ +, *first2+ +); } return D_first;}
Yes first1
, last1
and first2
two yuan after the operation binary_op
is saved in d_first
the starting position.
They can also be seen in their implementation.
Example:
#include<string>#include<CType. h>#include<algorithm>#include<Functional>#include<iostream>int Main(){ std:: String s ("Hello"); std::transform (S.begin (), S.end (), S.begin (),:: ToUpper) ; std:: cout << s; }
The output is:
HELLO
Example:
std::string s("hello"); std::string d("kkkkk"); std::transform(s.begin(), s.end(), d.begin::toupper); std::coutstd::endl; std::cout<<d<<std::endl;
The output is:
helloHELLO
Example:
std::string s("hello"); std::transform(s.begin(), s.end(), s.begin()+2::toupper); std::coutstd::endl;
The output is:
heHEH
Finally attach the source code
//TransformTemplate<class_inputiter,class_outputiter,class_unaryoperation>_outputiter transform (_inputiter __first, _inputiter __last, _outputiter __result, _unaryoperation __opr) {__stl_requires (_inputiter, _inputiterator); __stl_requires (_outputiter, _outputiterator); for(; __first! = __last; ++__first, ++__result) *__result = __OPR (*__first);return__result;}Template<class_inputiter1,class_inputiter2,class_outputiter,class_binaryoperation>_outputiter transform (_inputiter1 __first1, _inputiter1 __last1, _inputiter2 __fi Rst2, _outputiter __result, _binaryoperation __binary_op) {__stl_requires (_inputiter1, _inputiterat OR); __stl_requires (_inputiter2, _inputiterator); __stl_requires (_outputiter, _outputiterator); for(; __first1! = __last1; ++__first1, ++__first2, ++__result) *__result = __binary_op (*__first1, *__first2);return__result;}
Transform use of the detailed