The text above demonstrates the charm of copy. Now let's take a look at its twin brother copy_backward. The copy_backward algorithm is similar to the copy Algorithm in terms of behavior, but its copy process is different from the copy algorithm, the replication process starts from the last element until the first element is copied. That is to say, the replication operation starts from last-1 until the end of first. These elements are also copied from the back to the target container, and the last-first element is copied from result-1. A simple example: we know that vector {0, 1, 2, 3, 4, 5}. Now we need to put the last three elements (3,
4, 5) copy to the first three positions (0, 1, 2), then we can set this: Set first to the position where the value is 3, set last to the next position of 5, and result to the position of 3. In this way, the value 5 is copied to the position of 2, and then 4 is copied to the position of 1, copy the last 3 to the 0 position and obtain the sequence {3, 4, 5, 3, 4, 5 }. Let's take a look at the prototype of the copy_backward function:
Function prototype:
template<class BidirectionalIterator1, class BidirectionalIterator2> BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result);
Parameters:
First, last
Specifies the range of the copied element [first, last ).
Result
Specifies the specific location of the data to be copied to the target range [result-(last-first), result)
Return Value:
Returns an iterator indicating the starting position of the element range to be copied.
Program example:
First, we will use a simple example to describe how to use copy_backward. The program is simple and detailed in the code, so it is no longer cumbersome here.
/*************************************** * **************************** Copyright (c) jerry Jiang ** file name: copy_backward.cpp * Author: Jerry Jiang * Create Time: 23:14:57 * mail: jbiaojerry@gmail.com * blog: http://blog.csdn.net/jerryjbiao ** description: A simple program interprets the fourteen * Variable Algorithms in the C ++ STL algorithm series: reverse copy copy_backward ************************************* * *****************************/# InCl Ude <iostream> # include <algorithm> # include <vector> using namespace STD; int main () {vector <int> myvector; vector <int>: iterator ITER; // assign the initial value to the container myvector: 10 20 30 40 50for (INT I = 1; I <= 5; ++ I) {myvector. push_back (I * 10);} // increase the size of the myvector container to three units: myvector. resize (myvector. size () + 3); // copy the 20 and 10 elements of the container to Unit 8 and Unit 7: 10 20 30 40 50 0 10 20 // note that copy_backward is a reverse copy, copy 20 to the eighth unit, and then copy 10 to the seventh unit copy_backward (myvector. B Egin (), myvector. Begin () + 2, myvector. End (); For (iter = myvector. Begin (); iter! = Myvector. end (); ++ ITER) {cout <"" <* ITER;} cout <Endl; // clear the myvector container myvector. clear (); // restore the initial value of the container myvector: 10 20 30 40 50for (I = 1; I <= 5; ++ I) {myvector. push_back (I * 10);} // cover the 40, 50, and 20 elements of the container, that is, 40 50 30 40 50: copy_backward (myvector. end ()-2, myvector. end (), myvector. end ()-3); For (iter = myvector. begin (); iter! = Myvector. End (); ++ ITER) {cout <"" <* ITER;} cout <Endl; return 0 ;}
Through the simple introduction in the previous example, I believe that you are no longer familiar with the basic use of copy_backward. ^ _ ^. Next we will combine the for_search algorithm mentioned above to consolidate the use of copy_backward.
/*************************************** * **************************** Copyright (c) jerry Jiang ** file name: copy_backward02.cpp * Author: Jerry Jiang * Create Time: 2012-3-21 23:48:14 * mail: jbiaojerry@gmail.com * blog: http://blog.csdn.net/jerryjbiao ** description: A simple program interprets the fourteen * Variable Algorithms in the C ++ STL algorithm series: reverse copy copy_backward ************************************* * *****************************/# In Clude <iostream> # include <algorithm> # include <vector> # include <iterator> # include <string> using namespace STD; Class output_element {public: // overload operator () void operator () (string element) {cout <element <(_ line_cnt ++ % 7? "": "\ N \ t"); // format the output, that is, every 7 lines and tabs} static void reset_line_cnt () {_ line_cnt = 1;} PRIVATE: static int _ line_cnt;}; int output_element: _ line_cnt = 1; // defines and initial static data member int main () {string SA [] = {"", "light", "untonusred", "hair", "grained", "and", "hued", "like", "Pale", "Oak "}; vector <string> SVEC (SA, Sa + 10); // remember for_each, here it is used as the output // for_each specific usage reference http://blog.csdn.net/jerryjbiao/article/details/6827508cout <"original list of strings: \ n \ t"; for_each (SVEC. begin (), SVEC. end (), output_element (); cout <"\ n" <Endl; // set "the", "light", "untonusred", "hair ", "grained", // "and", "hued" three units after the move cover "like", "Pale", "Oak" copy_backward (SVEC. begin (), SVEC. end ()-3, SVEC. end (); output_element: reset_line_cnt (); cout <"sequence after" <"copy_backward (SVEC. begin (), SVEC. end ()-3, SVEC. end (): \ n \ t "; for_each (SVEC. begin (), SVEC. end (), output_element (); cout <"\ n" <Endl; return 0 ;}
**************************************** **************************************** **************************************** *******
C ++ classic bibliography index and resource download:Http://blog.csdn.net/jerryjbiao/article/details/7358796
**************************************** **************************************** **************************************** ********