C++ STL algorithm, rotate_copy(), integer program example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: none
To do: Using rotate_copy() to exchange the elements in two adjacent ranges within a source range and copies the result to a destination range in C++ programming
To show: How to use the C++ algorithm, rotate_copy() to exchange the elements in two adjacent ranges within a source range and copies the result to a destination range in C++ programming
// C++ STL algorithm, rotate_copy()
#include <vector>
#include <deque>
#include <algorithm>
#include <iostream>
using namespace std;
int main(void)
{
// vector and deque containers
vector <int> vec1, vec2(9);
deque <int> deq1, deq2(6);
// vector and deque iterators
vector <int>::iterator vec1Iter, vec2Iter;
deque<int>::iterator deq1Iter, deq2Iter;
int i, j, k=1;
// push data in range
for(i = -3; i <= 5; i++)
vec1.push_back(i);
// print the data
cout<<"vec1 vector data is: ";
for(vec1Iter = vec1.begin(); vec1Iter != vec1.end(); vec1Iter++)
cout<<*vec1Iter<<" ";
cout<<endl;
rotate_copy(vec1.begin(), vec1.begin() + 3, vec1.end(), vec2.begin());
cout<<"\nAfter rotating, the vec1 vector data remains unchanged as: ";
for(vec1Iter = vec1.begin(); vec1Iter != vec1.end(); vec1Iter++)
cout<<*vec1Iter<<" ";
cout<<endl;
// rotate and copy to vec2
cout<<"\nBut, after rotating, the copy of vec1 vector in vec2 is, vec2: ";
for(vec2Iter = vec2.begin(); vec2Iter != vec2.end(); vec2Iter++)
cout<<*vec2Iter<<" ";
cout<<endl;
// push data in range
for(j =0; j <= 5; j++)
deq1.push_back(j);
cout<<"\nThe original deq1 deque is: ";
for(deq1Iter = deq1.begin(); deq1Iter != deq1.end(); deq1Iter++)
cout<<*deq1Iter<<" ";
cout<<endl;
while(k <= deq1.end() - deq1.begin())
{
// just informational
cout<<"k = "<<k<<" deq1.end() - deq1.begin(): "<<(deq1.end() - deq1.begin())<<endl;
// rotate and copy to deq2
rotate_copy(deq1.begin(), deq1.begin() + k, deq1.end(), deq2.begin());
cout<<"\nRotation of a single deque element to the back, a deque copy, deq2 is: ";
for(deq2Iter = deq2.begin(); deq2Iter != deq2.end(); deq2Iter++)
cout<<*deq2Iter<<" ";
cout<<endl;
k++;
}
return 0;
}
Output examples:
vec1 vector data is: -3 -2 -1 0 1 2 3 4 5
After rotating, the vec1 vector data remains unchanged as: -3 -2 -1 0 1 2 3 4 5
But, after rotating, the copy of vec1 vector in vec2 is, vec2:0 1 2 3 4 5 -3 -2 -1
The original deq1 deque is: 0 1 2 3 4 5
k = 1 deq1.end() - deq1.begin(): 6
Rotation of a single deque element to the back, a deque copy, deq2 is: 1 2 3 4 5 0
k = 2 deq1.end() - deq1.begin(): 6
Rotation of a single deque element to the back, a deque copy, deq2 is: 2 3 4 5 0 1
k = 3 deq1.end() - deq1.begin(): 6
Rotation of a single deque element to the back, a deque copy, deq2 is: 3 4 5 0 1 2
k = 4 deq1.end() - deq1.begin(): 6
Rotation of a single deque element to the back, a deque copy, deq2 is: 4 5 0 1 2 3
k = 5 deq1.end() - deq1.begin(): 6
Rotation of a single deque element to the back, a deque copy, deq2 is: 5 0 1 2 3 4
k = 6 deq1.end() - deq1.begin(): 6
Rotation of a single deque element to the back, a deque copy, deq2 is: 0 1 2 3 4 5
Press any key to continue . . .