/*Template < class Inputiterator, class Outputiterator, Class Unaryoperator > Outputiterator transform (Inputiterator first1,//start address of source container inputiterator Last1,/ /End address of the source container outputiterator result,//start address of the target container Unaryopera Tor op); function pointer//typedef target CONTAINER element type (*unaryoperator) (source container element type); Template < class InputIterator1, class InputIterator2, Class Outputiterator, Class Binaryoperator > Outputiterator transform (InputIterator1 first1,//Starting address of Source container 1 InputIterator1 las T1,//termination address of source container 1 InputIterator2 first2,//start address of source container 2, number of elements same as 1 Outputiterator result,//destination container start address, number of elements same as 1 Binaryoperator binary_op ); function pointer//typedef target CONTAINER element type (*binaryoperator) (source container 1 element type, source capacity2 element type); //*//////////////////////////////// #include<iostream>#include<algorithm>#include<vector>#include<string>using namespacestd; intOp_increase (inti) {returni+1; } intOp_sum (intIintj) {returni+J; } intTo_upper (intc) {if(Islower (c)) {return(C- +); } returnC; } intTo_lower (intc) {if(Isupper (c)) {returnc+ +; } returnC; } intmain () {vector<int>First ; Vector<int>second; Vector<int>:: Iterator it; //set some values: for(intI=1; i<6; i++) First.push_back (i*Ten);//first:10 ///assigns the element of the first container plus 1 to the second containerSecond.resize (First.size ());//Allocate space!!! Must be pre-set to the same size as firstTransform (First.begin (), First.end (), Second.begin (), op_increase);//second:11cout <<"Second contains:"; for(It=second.begin (); It!=second.end (); + +it) {cout<<" "<< *it; } cout<<Endl; //*//////////////////////////////////////////// ///Adds the elements of the first container to the elements of the second container and assigns the resulting results back to the firstTransform (First.begin (), First.end (), Second.begin (), First.begin (), op_sum);//first:21, Bayi 101cout <<"First contains:"; for(It=first.begin (); It!=first.end (); + +it) cout<<" "<< *it; cout<<Endl; //*////////////////////////////////////////////////////////////////////////// ///uppercase and lowercase conversions///////////////////////////////////// stringSTRSRC ("Hello, world!."); stringstrdest; Strdest.resize (Strsrc.size ()); // !!! Must be pre-set to the same size as StrsrcTransform (Strsrc.begin (), Strsrc.end (), Strdest.begin (), to_upper);//Convert to uppercasecout << strdest <<Endl; Transform (Strsrc.begin (), Strsrc.end (), Strdest.begin (), to_lower); //Convert to lowercasecout << strdest <<Endl; //*///////////////////////////////////////// return 0; }
The transform of C++STD function