Algorithm for executing function calls on each element of a sequence by a generic algorithm (ii)

Source: Internet
Author: User

1, For_each (Initerbegin, Initerend, Ufunc): Invokes each element of the sequence in a function object Ufunc

For example, the entire element of the output sequence can be written like this:

std::vector<int> c;c.reserve ()//Add element to C  for (int 0 ten; i++) {    c.push_back (i);} Output c all elements Std::for_each (C.begin (), C.end (), [] (int  element) {    std::cout<< element<<",";});
Print Result: 0,1,2,3,4,5,6,7,8,9

2, transform (Inputiterator first1, Inputiterator last1, outputiterator result, unaryoperation op): Performs unary operation op for each element of the sequence, The result is written in another sequence

For example, by multiplying the elements in C1 by 10 and putting them in C2, you can write:

std::vector<int>C1; Std::vector<int>C2; C2.resize (Ten);//This must be there, specifically refer to clause 30 of the effective STL: Ensure that the target range is large enough//adding elements to C1     for(inti =0; I <Ten; i++) {c1.push_back (i); }    //multiply each element in the C1 by 10 and deposit it in C2Std::transform (C1.begin (), C1.end (), C2.begin (), [] (intElement) {        returnElement *Ten;    }); //elements in the output C2     for(Autovar: C2) {Std::cout<<var<<","; }
Print Result: 0,10,20,30,40,50,60,70,80,90

3, transform (InputIterator1 first1, InputIterator1 last1,inputiterator2 first2, Outputiterator result,binaryoperation BINARY_OP): Performs a two-binary_op operation on each pair of elements in the two sequence, and the result is written in another sequence

For example, the sum of the corresponding elements in the above c1,c2 is deposited into the C3, which can be written as follows:

std::vector<int>C1; Std::vector<int>C2; Std::vector<int>C3; C2.resize (Ten);//This must be there, specifically refer to clause 30 of the effective STL: Ensure that the target range is large enoughC3.resize (Ten); //adding elements to C1     for(inti =0; I <Ten; i++) {c1.push_back (i); }    //multiply each element in the C1 by 10 and deposit it in C2Std::transform (C1.begin (), C1.end (), C2.begin (), [] (intElement) {        returnElement *Ten;    }); //each element in the C1, C2, and stored in the C3Std::transform (C1.begin (), C1.end (), C2.begin (), C3.begin (), [] (intElement1,intElement2) {        returnElement1 +Element2;    });  for(Autovar: C3) {Std::cout<<var<<","; }    //Print Result: 0,11,22,33,44,55,66,77,88,99

Algorithm for executing function calls on each element of a sequence by a generic algorithm (ii)

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.