Transform use of the detailed

Source: Internet
Author: User

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;}

first1a 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.