The first twelve algorithms are all presented in the non-mutating algorithms series. Let's take a look at the variable algorithms. Mutating algorithms is a set of template functions that can modify container element data. It can replicate and transform sequence data.
Let's take a look at the first variable algorithm: Copy of the element copy algorithm. This algorithm is mainly used to copy elements between containers, that is, copying the elements in the iterator range [first, last) to the range [result, result + (last-first) given by the replication target result )). Let's take a look at its function prototype:
Original function:
template<class InputIterator, class OutputIterator> OutputIterator copy( InputIterator _First, InputIterator _Last, OutputIterator _DestBeg );
Parameters
-
_ First, _ last
-
Specifies the range of the copied element [_ first, _ last ).
-
_ Destbeg
-
Specifies the start position of the destination range to be copied.
Return Value
Returns an iterator indicating the last position of the element range to be copied.
Program example:
First, let's take a simple example to define a simple integer array myints, copy all its elements to the container myvector, and move the array to the left.
/*************************************** * **************************** Copyright (c) jerry Jiang ** file name: copy01.cpp * Author: Jerry Jiang * Create Time: 22:44:28 * mail: jbiaojerry@gmail.com * blog: http://blog.csdn.net/jerryjbiao ** description: simple programs interpret C ++ STL algorithm series 13 * Variable Algorithms: copy an element ************************************* * *****************************/# include <iostream> # I Nclude <algorithm> # include <vector> using namespace STD; int main () {int myints [] = {10, 20, 30, 40, 50, 60, 70 }; vector <int> myvector; vector <int>: iterator it; myvector. resize (7); // allocate space for the container myvector // copy usage 1: // copy the seven elements in the array myints to the myvector container copy (myints, myints + 7, myvector. begin (); cout <"myvector contains:"; for (IT = myvector. begin (); it! = Myvector. end (); ++ it) {cout <"" <* it;} cout <Endl; // copy usage 2: // move the elements in the array myints to the left to copy (myints + 1, myints + 7, myints); cout <"myints contains:"; for (size_t I = 0; I <7; ++ I) {cout <"" <myints [I];} cout <Endl; return 0 ;}
From the above example, we can see that the copy algorithm can easily copy the elements in a container to another target container,In the code above, note that myvector. Resize (7) is used. In this line of code, we must allocate space for the vector first. Otherwise, the program will collapse, which is a common mistake for beginners.In fact, the greatest power of the copy function is that when combined with the standard input/output iterator, we can see its power through the following example.
/*************************************** * **************************** Copyright (c) jerry Jiang ** file name: copy2.cpp * Author: Jerry Jiang * Create Time: 23:25:29 * mail: jbiaojerry@gmail.com * blog: http://blog.csdn.net/jerryjbiao ** description: simple programs interpret C ++ STL algorithm series 13 * Variable Algorithms: copy an element ************************************* * ****************************/# include <iostream> # include <Algorithm> # include <vector> # include <iterator> # include <string> using namespace STD; int main () {typedef vector <int> intvector; typedef istream_iterator <int> istreamitr; typedef ostream_iterator <int> ostreamitr; typedef struct <intvector> backinsitr; intvector myvector; // read the integer from the standard input device // enter the integer sequence until the input is non-integer. Press any non-numeric key and press enter to end the input cout <"Please input element: "<Endl; copy (istreamitr (CIN), istreamitr (), backinsitr (myvector); // all elements in the output container, elements are separated by spaces. <"output:" <Endl; copy (myvector. begin (), myvector. end (), ostreamitr (cout, ""); cout <Endl; return 0 ;}
Well, now we know what the magic of copy is. ^ _ ^
**************************************** **************************************** **************************************** *******
C ++ classic bibliography index and resource download:Http://blog.csdn.net/jerryjbiao/article/details/7358796
**************************************** **************************************** **************************************** ********