I have forgotten

• https://me.yahoo.com

# rotate_copy

Copies the elements while rotating their order
View version details

### Key Facts

Gyroscopic Couple: The rate of change of angular momentum () = (In the limit).
• = Moment of Inertia.
• = Angular velocity
• = Angular velocity of precession.

Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.

Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.

## Definition

The rotate_copy() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.

## Interface

#include <algorithm>
template < class ForwardIterator, class OutputIterator >
OutputIterator rotate_copy(
ForwardIterator first,
ForwardIterator middle,
ForwardIterator last,
OutputIterator result
);

Parameters:
Parameter Description
first A forward iterator addressing the position of the first element in the range to be rotated
middle A forward iterator defining the boundary within the range that addresses the position of the first element in the second part of the range whose elements are to be exchanged with those in the first part of the range
last A forward iterator addressing the position one past the final element in the range to be rotated
result An output iterator addressing the position of the first element in the destination range

## Description

Rotate_copy function make a rotated copy of a sequence. It means the resulting sequence will start at the element in the middle of the source sequence and the last element will be followed by the first (it appends the element between the fi rst and the middle element to the elements between the middle and the last element).

## Return Value

Returns an iterator on the element following the last copied element.

## Complexity

The complexity is linear; performs last - first assignments.

### References

Example:
##### Example - rotate_copy algorithm
Problem
This program illustrates the use of the STL rotate_copy() algorithm to rotate the order of all, or just some, of the values in a vector of integers while copying the values to an output range in another vector of integers.
Workings
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
int a1[] = {1, 2, 3, 4, 5};
vector<int> v1(a1, a1+5);

cout <<"\nHere are the values in the vector v1:\n";
for (vector<int>::size_type i=0; i<v1.size(); i++)
cout <<v1.at(i)<<" ";

int a2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector<int> v2(a2, a2+10);

cout <<"\nHere are the values in the vector v2:\n";
for (vector<int>::size_type i=0; i<v2.size(); i++)
cout <<v2.at(i)<<" ";

cout <<"\nNow we copy all values from v1 to v2, starting at the "
"beginning of v2,\nand simultaneously rotating the order of the "
"copied values so that the\nfourth value bcomes the first value.";

vector<int>::iterator p;
p = rotate_copy(v1.begin(), v1.begin()+3, v1.end(), v2.begin());

cout <<"\nHere are the revised values in v2:\n";
for (vector<int>::size_type i=0; i<v2.size(); i++)
cout <<v2.at(i)<<" ";

cout <<"\nThe iterator returned by the algorithm is pointing at the value"
<<*p<<".";

cout <<"\nNow we copy the midddle 3 of the values in v1 from v1 to "
"v2, starting at the\n7th value of v2, and simultaneously rotate "
"the order of the copied values so\nthat the 3rd of the copied "
"values becomes the first of the copied group.";

p = rotate_copy(v1.begin()+1, v1.begin()+3, v1.end()-1, v2.begin()+6);
cout <<"\nHere are the revised values in v2:\n";
for (vector<int>::size_type i=0; i<v2.size(); i++)
cout <<v2.at(i)<<" ";

cout <<"\nThe iterator returned by the algorithm is pointing at the value"
<<*p<<".";

return 0;
}
Solution
Output:

Here are the values in the vector v1:
1 2 3 4 5

Here are the values in the vector v2:
1 2 3 4 5 6 7 8 9 10

Now we copy all values from v1 to v2, starting at the beginning of v2,
and simultaneously rotating the order of the copied values so that the
fourth value bcomes the first value.

Here are the revised values in v2:
4 5 1 2 3 6 7 8 9 10

The iterator returned by the algorithm is pointing at the value6.

Now we copy the midddle 3 of the values in v1 from v1 to v2, starting at the
7th value of v2, and simultaneously rotate the order of the copied values so
that the 3rd of the copied values becomes the first of the copied group.

Here are the revised values in v2:
4 5 1 2 3 6 4 2 3 10

The iterator returned by the algorithm is pointing at the value10.
References