Home > Back-end >  Questions about C copy_backward STL algorithms.
Questions about C copy_backward STL algorithms.

Time:09-22

 
//the elements in the container myvector for 10,20,30,40,50
//the container element, 40, 50 covers 10, 20, namely: 40 to 50 30 to 40 to 50:
Copy_backward (myvector. End () - 2, myvector. The end (), myvector. The end () - 3);
//why myvector. End () - 2 is 40 myvector. End () - 3 is 20,,,




The function prototype is as follows,
 
TemplateBidirectionalIterator2 copy_backward (BidirectionalIterator1 first,
BidirectionalIterator1 last,
BidirectionalIterator2 result);


Want to know whether [first, last) interval,

There is a problem about the begin () and end (),


The begin () points to the element, the end () is the tail after such as: there are 10 20 30 to 40 to 50
Vector MAC;
MAC. The begin () + 4, etc is not equal to the MAC. The end () - 1? Is 50.?

Or as I write this

Copy_backward (myvector. End () - 2, myvector. The end (), myvector. The end () - 3);
If the three parameters are transposed end (),
Copy_backward (myvector end (), myvector. The end (), myvector. The end ());
As the first parameter is the first, and the second parameter is the last), the two myvector. The end () equal?

CodePudding user response:

To solve! To solve! To solve!

CodePudding user response:

Look at the document (also can see the source code) http://www.cplusplus.com/reference/algorithm/copy_backward/? Kw=copy_backward
The function begins by copying * (last) into * (result - 1), and then follows backward by The elements preceding these, until The first is reached (and o it).

Begin () and end () the problem is that, in order to ensure that the classical iteration written
While (it!=the container. The end ()) + + it; To gm and the last available elements of iterator from when it reaches the end (position), the add
The vector iterator is a pointer, so what exactly do v.begin () + (v. considering () - 1) and v.e nd () - 1 are equal,
  • Related