C + + Transform

Source: Internet
Author: User

The transform function works by applying an action to each element of a specified range. There are two overloaded versions of the transform function:
Transform (FIRST,LAST,RESULT,OP);//first is the first iterator of the container, and last is the end iterator of the container, result is the container that holds the results, OP is the unary function object or STURCT, class to be manipulated.

Transform (FIRST1,LAST1,FIRST2,RESULT,BINARY_OP);//first1 is the first iterator to a container, Last1 is the last iterator to the first container, and First2 is the first iterator of the second container, Result is the container that holds the result, Binary_op is the two-tuple function object or STURCT, class to be manipulated.

Note: The second overloaded version must ensure that the number of elements in the two container is equal, otherwise an exception will be thrown.

Look at an example: Use the transform function to rewrite lowercase letters in a given string into uppercase letters and save the result in an array called second, with the original string content intact.
We just need to use the first overloaded function of transform, and of course we can use the For_each function to copy a few more times, and now look at the code:

1#include <iostream>2#include <algorithm>3 using namespacestd;4 CharOpCharch)5 {6  7    if(ch>='A'&&ch<='Z')8         returnch+ +;9     ElseTen         returnch; One } A intMain () - { -     stringFirst,second; theCin>>First ; - second.resize (First.size ()); - transform (First.begin (), First.end (), Second.begin (), op); -cout<<second<<Endl; +     return 0; -}

Take Another example: give you two vector vectors (the number of elements is equal), you can use the transform function to multiply each element of two vectors, and output the result of multiplying.
Code:usage of foreach
1#include <iostream>2#include <algorithm>3#include <vector>4 using namespacestd;5 voidPrintint&elem) {cout<<elem<<" ";}6 intOpintAintb) {returnA *b;}7 intMain ()8 {9Vector <int>a,b,sum;Ten     intN; OneCin>>N; A      for(intI=0; i<n;i++) -     { -         intT; theCin>>T; - A.push_back (t); -     } -      for(intI=0; i<n;i++) +     { -         intT; +Cin>>T; A B.push_back (t); at     } - sum.resize (n); - transform (A.begin (), A.end (), B.begin (), Sum.begin (), op); - For_each (Sum.begin (), Sum.end (), print); -     return 0; -}

C + + Transform

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.