STL Algorithm 2

Source: Internet
Author: User

Modified algorithm

First, copy

Copy (Myvector.begin (), Myvector.end (), L1.begin ()), L1 must be capacity before copying, or the system will error

Copy_backward (Myvector.begin (), Myvector.end (), L1.end ()), and in L1 is copied from the back forward

Copy (Myvector.begin (), Myvector.end (),ostream_iterator<int> (cout, ",")), comma-delimited output

Second, transform ()

Simultaneous replication and transformation

Transform (V1.begin (), V1.end (), Back_inserter (L2), negate<int> ());//Multiply the number in V1 by-1, copy it into L2, and Back_inserter is the back iterator, You can copy without initializing capacity
cout << "List2" << Endl;
For_each (L2.begin (), L2.end (), print);
cout << Endl;
cout << "elements in L2 multiplied by ten" << Endl;
Transform (L2.begin (), L2.end (), L2.begin (), bind2nd (Multiplies<int> (), 10));
For_each (L2.begin (), L2.end (), print);
cout << Endl;
Transform (L2.begin (), L2.end (), ostream_iterator<int> (cout, ""), bind2nd (Divides<int> (), 2));// The elements in the L2 do not change, just the output
cout << Endl;
Transform (L2.begin (), L2.end (), L2.begin (), ostream_iterator<int> (cout, ","), multiplies<int> ());// The elements in the L2 and the elements in the L2 are multiplied separately by the output

Iii. Exchange

Swap (v1, v2);//Exchange V1,v2

Swap_ranges (V1.begin (), V1.begin () + 5, V2.begin ());//exchange of elements of a certain range

Iv. filling elements

Fill (V1.begin (), V1.begin () + 4, 9),//from v1[0] to V1[4] becomes 9

Fill_n (V1.begin (), 5,//v1[0] to v1[4] becomes 20

Generate (V2.begin (), V2.end (), Rand);//Generate random numbers

Generate (V2.begin (), 3,rand);//generate 3 random numbers

V. Replacement

Replace (V1.begin (), V1.end (), 7),//v1[6] becomes 99

Replace_if (V1.begin (), V1.end (), bind2nd (Less<int> (), 10), 11);//The judging condition is true replace

Vi. reversal

Reverse (V1.begin (), V1.end ());

Reverse_copy (V1.begin (), V1.end (),ostream_iterator<int> (cout, ",");//Reverse Output

Seven, rotation

Rotate (V1.begin (), V1.begin () +4,v1.end ());

Rotate_copy (V1.begin (), V1.begin () +3,v1.end (),ostream_iterator<int> (cout, ",");

STL Algorithm 2

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.